Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 911
Bruno Pedroso Avatar asked Oct 21 '11 14:10

Bruno Pedroso


2 Answers

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

like image 112
Wojtek Kruszewski Avatar answered Oct 12 '22 23:10

Wojtek Kruszewski


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.

like image 23
Gaurav Saini Avatar answered Oct 12 '22 23:10

Gaurav Saini