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 causes 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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With