<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 think Jarkko is saying that no spec should have been written for the code I was trying to fix/change.&nbsp; I'm not sure I would agree that BDD/RSpec is an inappropriate tool for documenting what I was trying to change, but I think he would argue that....<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: David Chelimsky &lt;dchelimsky@gmail.com&gt;<br>To: rspec-users &lt;rspec-users@rubyforge.org&gt;<br>Sent: Tuesday, December 4, 2007 12:18:58 PM<br>Subject: Re: [rspec-users] params not available for controller specs?<br><br>
On Dec 4, 2007 2:09 PM, Al Chou &lt;<a ymailto="mailto:hotfusionman@yahoo.com" href="mailto:hotfusionman@yahoo.com">hotfusionman@yahoo.com</a>&gt; wrote:<br>&gt;<br>&gt; I get what you're saying, but I was trying to fix a bug in existing
 code in<br>&gt; Substruct (that I did not write) that was caused by Rails passing the
 array<br>&gt; as a /-delimited string and then not automatically decoding that
 string.&nbsp; As<br>&gt; Substruct does not say that Edge Rails is a requirement, I felt it
 was worth<br>&gt; documenting what I had to change to run it on Rails 1.2.x.&nbsp; Every
 code<br>&gt; change should be driven by a test or example; perhaps in this
 situation I<br>&gt; should've gone over to Test::Unit instead of staying in RSpec?&nbsp; An<br>&gt; interesting philosophical thought....<br><br>Seems to me this thread has been about how to deal with rails. I don't<br>see what that has to do w/ a T::U vs rspec decision.<br><br>&gt;<br>&gt; Al<br>&gt;<br>&gt;<br>&gt;<br>&gt; ----- Original Message ----<br>&gt; From: Jarkko Laine &lt;<a ymailto="mailto:jarkko@jlaine.net" href="mailto:jarkko@jlaine.net">jarkko@jlaine.net</a>&gt;<br>&gt; To: rspec-users &lt;<a ymailto="mailto:rspec-users@rubyforge.org" href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a>&gt;<br>&gt; Sent: Tuesday, December 4, 2007 10:09:01 AM<br>&gt; Subject: Re: [rspec-users] params not available for controller specs?<br>&gt;<br>&gt;<br>&gt; On 4.12.2007, at 18.19, Al Chou wrote:<br>&gt;<br>&gt; &gt; Going back to my original message, I have the following at the<br>&gt; &gt; beginning of the download method:<br>&gt;
 &gt;<br>&gt; &gt;&nbsp; def download<br>&gt; &gt;&nbsp; &nbsp; @orders = Order.find( params[:ids] )<br>&gt; &gt;&nbsp; &nbsp; ...<br>&gt; &gt;<br>&gt; &gt; The problem is that params[:ids], although built as an Array object<br>&gt; &gt; by the view that calls the download method on the controller, is<br>&gt; &gt; passed as a /-delimited string of id values.<br>&gt;<br>&gt; If I set<br>&gt;<br>&gt;&nbsp;  &lt;%= link_to "Test", :controller =&gt; "clients", :foo =&gt; [1, 2, 3, 4,<br>&gt; 5] %&gt;<br>&gt;<br>&gt; I get this as the url:<br>&gt;<br>&gt;
 <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>&gt;<br>&gt; That will get correctly parsed back to an array in the receiving
 action:<br>&gt;<br>&gt; Parameters: {"action"=&gt;"index", "foo"=&gt;["1", "2", "3", "4", "5"],<br>&gt; "controller"=&gt;"clients", "locale"=&gt;"en"}<br>&gt;<br>&gt; This is Edge Rails, though, so YMMV.<br>&gt;<br>&gt; &gt;&nbsp; Order.find() will not do the desired thing with that string, which<br>&gt; &gt; was intended to be an array by the view, but Rails passed it in a<br>&gt; &gt; string representation.&nbsp; So the method really should be<br>&gt; &gt;<br>&gt; &gt;&nbsp; def download<br>&gt; &gt;&nbsp; &nbsp; @orders = Order.find( params[:ids].split( '/' ) )<br>&gt; &gt;<br>&gt; &gt; and what I'm trying to spec is the addition of the call to split().<br>&gt;<br>&gt; IMHO there's no need to stub string methods like that. Just do<br>&gt; something like this:<br>&gt;<br>&gt; Order.should_receive(:find).with(%w(1 2<br>&gt; 3)).and_return(@some_order_objects)<br>&gt;<br>&gt; If params[:ids] is "1/2/3", you can be pretty certain that split("/")<br>&gt;
 will work correctly so you're more interested in that the find method<br>&gt; gets called with correct set of attributes, an array. It's not really<br>&gt; interesting how that array got to be. In this case, you might notice<br>&gt; that somehow params[:ids] is an array after all and you can take the<br>&gt; split call away, and the spec will still pass.<br>&gt;<br>&gt; I guess what I'm trying to say is that the split call is part of the<br>&gt; controller action's internal implementation, and you should be<br>&gt; spec'ing how it behaves related to its surrounding world: fetches<br>&gt; stuff from the business model, assigns objects for the view etc.<br>&gt;<br>&gt; //jarkko<br>&gt;<br>&gt; --<br>&gt; Jarkko Laine<br>&gt; <a href="http://jlaine.net" target="_blank">http://jlaine.net</a><br>&gt; <a href="http://dotherightthing.com" target="_blank">http://dotherightthing.com</a><br>&gt; <a href="http://www.railsecommerce.com"
 target="_blank">http://www.railsecommerce.com</a><br>&gt; <a href="http://odesign.fi" target="_blank">http://odesign.fi</a><br>&gt;<br>&gt;<br>&gt; _______________________________________________<br>&gt; rspec-users mailing list<br>&gt; <a ymailto="mailto:rspec-users@rubyforge.org" href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>&gt; <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>