making two requests to the same controller in rails integrations specs

后端 未结 5 2040
鱼传尺愫
鱼传尺愫 2021-02-12 19:17

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=>\'         


        
相关标签:
5条回答
  • 2021-02-12 19:32

    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

    0 讨论(0)
  • 2021-02-12 19:38

    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

    0 讨论(0)
  • 2021-02-12 19:45

    You have to clear your instance variables, or maybe the only necessary one. Let's pretend you use @book in your controller.

    get '/something'
    assert ...
    controller.instance_variable_set(:@book, nil)
    get '/something'
    assert ...
    

    If you are using inherit_resources

    get '/something'
    assert ...
    controller.send(:set_resource_ivar, nil)
    get '/something'
    assert ...
    
    0 讨论(0)
  • 2021-02-12 19:47

    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.

    0 讨论(0)
  • 2021-02-12 19:53

    I don't think the problem is that you are getting the same response, it is that you are actually sending the same request twice.

    The following works for me when I want to make two requests in the same test: @request.delete_header 'RAW_POST_DATA'

    0 讨论(0)
提交回复
热议问题