<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:garamond,new york,times,serif;font-size:12pt">I  get what you're saying, but I was trying to fix a bug in existing code in Substruct (that I did not write) that was caused by Rails passing the array as a /-delimited string and then not automatically decoding that string.&nbsp; As Substruct does not say that Edge Rails is a requirement, I felt it was worth documenting what I had to change to run it on Rails 1.2.x.&nbsp; Every code change should be driven by a test or example; perhaps in this situation I should've gone over to Test::Unit instead of staying in RSpec?&nbsp; An interesting philosophical thought....<br><br>Al<div><div style="font-family: garamond,new york,times,serif; font-size: 12pt;"><br><br><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;">----- Original Message ----<br>From: Jarkko Laine
 &lt;jarkko@jlaine.net&gt;<br>To: rspec-users &lt;rspec-users@rubyforge.org&gt;<br>Sent: Tuesday, December 4, 2007 10:09:01 AM<br>Subject: Re: [rspec-users] params not available for controller specs?<br><br>On 4.12.2007, at 18.19, Al Chou wrote:<br><br>&gt; Going back to my original message, I have the following at the&nbsp; <br>&gt; beginning of the download method:<br>&gt;<br>&gt;&nbsp;  def download<br>&gt;&nbsp; &nbsp; @orders = Order.find( params[:ids] )<br>&gt;&nbsp; &nbsp; ...<br>&gt;<br>&gt; The problem is that params[:ids], although built as an Array object&nbsp; <br>&gt; by the view that calls the download method on the controller, is&nbsp; <br>&gt; passed as a /-delimited string of id values.<br><br>If I set<br><br>&nbsp;  &lt;%= link_to "Test", :controller =&gt; "clients", :foo =&gt; [1, 2, 3, 4,&nbsp; <br>5] %&gt;<br><br>I get this as the url:<br><br><a
 href="http://localhost:3000/en/clients?foo%5B%5D=1&amp;foo%5B%5D=2&amp;foo%5B%5D=3&amp;foo%5B%5D=4&amp;foo%5B%5D=5" target="_blank">http://localhost:3000/en/clients?foo%5B%5D=1&amp;foo%5B%5D=2&amp;foo%5B%5D=3&amp;foo%5B%5D=4&amp;foo%5B%5D=5</a><br><br>That will get correctly parsed back to an array in the receiving
 action:<br><br>Parameters: {"action"=&gt;"index", "foo"=&gt;["1", "2", "3", "4", "5"],&nbsp; <br>"controller"=&gt;"clients", "locale"=&gt;"en"}<br><br>This is Edge Rails, though, so YMMV.<br><br>&gt;&nbsp;  Order.find() will not do the desired thing with that string, which
&nbsp; <br>&gt; was intended to be an array by the view, but Rails passed it in a&nbsp; <br>&gt; string representation.&nbsp; So the method really should be<br>&gt;<br>&gt;&nbsp;  def download<br>&gt;&nbsp; &nbsp; @orders = Order.find( params[:ids].split( '/' ) )<br>&gt;<br>&gt; and what I'm trying to spec is the addition of the call to split().<br><br>IMHO there's no need to stub string methods like that. Just do&nbsp; <br>something like this:<br><br>Order.should_receive(:find).with(%w(1 2&nbsp; <br>3)).and_return(@some_order_objects)<br><br>If params[:ids] is "1/2/3", you can be pretty certain that split("/")&nbsp; <br>will work correctly so you're more interested in that the find method&nbsp; <br>gets called with correct set of attributes, an array. It's not really&nbsp; <br>interesting how that array got to be. In this case, you might notice&nbsp; <br>that somehow params[:ids] is an array after all and you can take the&nbsp; <br>split call away, and
 the spec will still pass.<br><br>I guess what I'm trying to say is that the split call is part of the&nbsp; <br>controller action's internal implementation, and you should be&nbsp; <br>spec'ing how it behaves related to its surrounding world: fetches&nbsp; <br>stuff from the business model, assigns objects for the view etc.<br><br>//jarkko<br><br>--<br>Jarkko Laine<br><a href="http://jlaine.net" target="_blank">http://jlaine.net</a><br><a href="http://dotherightthing.com" target="_blank">http://dotherightthing.com</a><br><a href="http://www.railsecommerce.com" target="_blank">http://www.railsecommerce.com</a><br><a href="http://odesign.fi" target="_blank">http://odesign.fi</a><br><br><br>_______________________________________________<br>rspec-users mailing list<br><a ymailto="mailto:rspec-users@rubyforge.org" href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/rspec-users"
 target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users</a></div></div></div></div><br>



      <hr size=1>Never miss a thing.  <a href="http://us.rd.yahoo.com/evt=51438/*http://www.yahoo.com/r/hs"> Make Yahoo your homepage.</a>

</body></html>