<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. 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 <dchelimsky@gmail.com><br>To: rspec-users <rspec-users@rubyforge.org><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 <<a ymailto="mailto:hotfusionman@yahoo.com" href="mailto:hotfusionman@yahoo.com">hotfusionman@yahoo.com</a>> wrote:<br>><br>> I get what you're saying, but I was trying to fix a bug in existing
code in<br>> Substruct (that I did not write) that was caused by Rails passing the
array<br>> as a /-delimited string and then not automatically decoding that
string. As<br>> Substruct does not say that Edge Rails is a requirement, I felt it
was worth<br>> documenting what I had to change to run it on Rails 1.2.x. Every
code<br>> change should be driven by a test or example; perhaps in this
situation I<br>> should've gone over to Test::Unit instead of staying in RSpec? An<br>> 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>><br>> Al<br>><br>><br>><br>> ----- Original Message ----<br>> From: Jarkko Laine <<a ymailto="mailto:jarkko@jlaine.net" href="mailto:jarkko@jlaine.net">jarkko@jlaine.net</a>><br>> To: rspec-users <<a ymailto="mailto:rspec-users@rubyforge.org" href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a>><br>> Sent: Tuesday, December 4, 2007 10:09:01 AM<br>> Subject: Re: [rspec-users] params not available for controller specs?<br>><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>