Friday, March 30, 2012

Rails 3. and rspec not doing what I expected

I'm trying to be better, at writing tests.  When I develop, especially for myself, I don't always know exactly how something will work or fit together exactly so I search with code for a while to find what I want.  It's hard to write a "red-green" test for this style.  I'm also not used to it as a lot of the work I have done in my past didn't have a test bed to do these kinds of tests.  So I'm bad at getting myself to write tests....blah, blah

So I was actually writing some tests with rspec and rails and they were failing, and I'm not expert at either rspec or rails and so a lot of things seem like "magic", ie get :index and suddenly a response variable shows up.

Anyway, I was testing a :post command and it should have been rendering a page after that.  I was trying to test the page rendered after the post.

response.should have_selector("form", :method => "post") do |form|          
   form.should have_selector("div", :class => "actions") do |div|            
     div.should have_selector("input", :value => "Create")
   end
end


And it just wasn't working, I was testing for information in the rendered page and it just wasn't there.  I dumped the response variable (puts response.inspect) and there was a lot of info there but clearly not the page I was expecting.  Then I dumped the header or head or something, can't remember and it clearly showed text that I was going to save but didn't, sorry, that said it was redirecting me.  Ahh! The answer, the post throws me to a redirect and I'm testing that page, it all makes sense! So now I'm testing that the redirect is going to the right spot and the other pages are tested a different way.

response.should redirect_to(subscribe_listing_purchases_path(Listing.last))

The end.

No comments:

Post a Comment