session available in some rspec files and not others. how come?

后端 未结 2 1492
南方客
南方客 2020-12-16 20:35

In trying to test some sign-in/out functionality I want to delete some information from the session. I found out I couldn\'t access the sessions at all. I kept getting the

相关标签:
2条回答
  • 2020-12-16 20:59

    You are testing two different things. In the first test you test a Rails controller, so RSpec adds helpers like session for you. In the second test, you're in a request test, so RSpec does not add any controller related helpers.

    UPDATE

    I have not tested this, but looking at the source code I think this should suffice

    include ::RSpec::Rails::ControllerExampleGroup
    

    For more info check out the rspec-rails project on github.

    UPDATE 2

    You should not test for cookies in request specs, quoting the RSpec docs:

    Request specs provide a thin wrapper around Rails' integration tests, and are designed to drive behavior through the full stack, including routing (provided by Rails) and without stubbing (that's up to you).

    0 讨论(0)
  • 2020-12-16 21:03

    Your 2nd test is a "request" spec which is an integration test. These are designed to simulate the browser and the helpers you get let you click buttons and fill out forms and assert text, tags on the page. It's the wrong level of abstraction for inspecting the session object.

    If you want to stub out authentication, e.g. best to go through the app. See, e.g. here: Stubbing authentication in request spec

    Know that integration tests are "exploratory" or "smoke" tests, high level tests that check the seams between components, not the guts of the components themselves. They're the most expensive to write and maintain. Use controller specs for verifying session stuff and move all business logic to the models where it's easiest to test.

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