making two requests to the same controller in rails integrations specs

I'm having problem making two requests to the same url in a rails integration test, with rspec

it 'does something' do # get '/something', {:status=>'any_other'}, @header ## <<<<< this line couses problem! get '/something', {:status=>'ok'}, @header doc = Nokogiri::HTML(response.body) lis = doc.css('#the_id') lis.size.should == 1 lis[0].text.should include('anything') end

If I make two requests to the same controller, the test seems to maintain the old response...

In the above example, if I uncomment that line, the test breaks beacause it maintains the result of the first 'query'

Is it a limitation of the test stack, or am I doing something wrong?

--------------Solutions-------------

Using Capybara instead of rspec is a better solution for (request) intergration tests. It uses the same syntax as rspec and allow multiple requests in a single it block. I use rspec for unit testing and capybara for integration testing.

https://github.com/jnicklas/capybara

With plain old Rails test suite, functional tests are for single request and if you want to test flows you should use integration tests (you can reset the controller in functional tests).

Controller specs from rspec-rails inherit from Rails functional tests, so they have same limitation. You can use rspec with capybara or webrat (I recommend the former) for integration tests.

Also, recent versions of rspec-rails has "request specs" which "mix in behaivour of Rails integration tests": https://github.com/rspec/rspec-rails

rails integration tests should be written so that the on case tests the one single request - response cycle. we can check redirects. but if you have to do something like

get '/something', {:status=>'any_other'}, @header

get '/something', {:status=>'ok'}, @header

You should write two different cases for this.

Category:ruby on rails Time:2011-10-21 Views:0

Related post

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.566 (s). 13 q(s)