Search code examples
ruby-on-railsrspecrspec-rails

How to move from requests to features in RSpec


I have installed a rspec-rails 3.0.0.beta1 (on ruby2 + rails4) and had some troubles using devise helper in my request specs. After some googling, I've found that I need to move all my specs from spec/requests to spec/features (the requests dir was created by rspec installator or scaffold generator [not sure right now], so I'm a bit confused). That made my devise helper working but there are more issues instead.

Here are three scenarios:

  1. Spec file is spec/requests/events_spec.rb and dont have any type set

    undefined method 'visit' for #<RSpec::ExampleGroups::Events::GETEvents:0x007ff2464d9848>
    
  2. Spec file is spec/requests/events_spec.rb and has a type: :controller

    it throws an error undefined method 'events_path' for nil:NilClass when i'm trying to use a get events_path method(s)

  3. Spec file is spec/features/events_spec.rb and dont have any type set

    undefined method `get' for #<RSpec::ExampleGroups::Events::GETEvents:0x007ffb2714a968>
    
  4. Spec file is spec/features/events_spec.rb and have a type: :controller

    undefined method `events_path' for nil:NilClass
    

I think I can find some tweaks on the internet but I'm a fresh rspec user and I feel like I'm doing something extremely wrong. And all the examples online are not related to my problem.

The code is here: https://gist.github.com/mbajur/8002303


Solution

  • As of Capybara 2.0, the Capybara methods (e.g. visit) are only available by default for feature specs, not request specs or controller specs.

    Similarly, the get method is not available for feature specs, only controller and request specs.

    Finally, the path helper methods are only available in request or feature specs.

    Given that, your failures can be explained as follows:

    1. No Capybara, because it's not a feature spec.
    2. No path helper methods because it's a controller spec.
    3. No get method because it's a feature spec.
    4. No path helper method because it's a controller spec.

    You need to either:

    • Make it a request spec and configure RSpec to include Capybara::DSL for request specs
    • Leave it a feature spec and stop using methods like get which aren't intended for use with feature specs

    Here's some interesting background on introducing feature specs as distinct from request specs