<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. 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. 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? 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
<jarkko@jlaine.net><br>To: rspec-users <rspec-users@rubyforge.org><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>> Going back to my original message, I have the following at the <br>> beginning of the download method:<br>><br>> def download<br>> @orders = Order.find( params[:ids] )<br>> ...<br>><br>> The problem is that params[:ids], although built as an Array object <br>> by the view that calls the download method on the controller, is <br>> passed as a /-delimited string of id values.<br><br>If I set<br><br> <%= link_to "Test", :controller => "clients", :foo => [1, 2, 3, 4, <br>5] %><br><br>I get this as the url:<br><br><a
href="http://localhost:3000/en/clients?foo%5B%5D=1&foo%5B%5D=2&foo%5B%5D=3&foo%5B%5D=4&foo%5B%5D=5" target="_blank">http://localhost:3000/en/clients?foo%5B%5D=1&foo%5B%5D=2&foo%5B%5D=3&foo%5B%5D=4&foo%5B%5D=5</a><br><br>That will get correctly parsed back to an array in the receiving
action:<br><br>Parameters: {"action"=>"index", "foo"=>["1", "2", "3", "4", "5"], <br>"controller"=>"clients", "locale"=>"en"}<br><br>This is Edge Rails, though, so YMMV.<br><br>> Order.find() will not do the desired thing with that string, which
<br>> was intended to be an array by the view, but Rails passed it in a <br>> string representation. So the method really should be<br>><br>> def download<br>> @orders = Order.find( params[:ids].split( '/' ) )<br>><br>> 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 <br>something like this:<br><br>Order.should_receive(:find).with(%w(1 2 <br>3)).and_return(@some_order_objects)<br><br>If params[:ids] is "1/2/3", you can be pretty certain that split("/") <br>will work correctly so you're more interested in that the find method <br>gets called with correct set of attributes, an array. It's not really <br>interesting how that array got to be. In this case, you might notice <br>that somehow params[:ids] is an array after all and you can take the <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 <br>controller action's internal implementation, and you should be <br>spec'ing how it behaves related to its surrounding world: fetches <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>