RSpec: Include a custom helper module in spec_help

2019-05-21 12:49发布

问题:

I am trying to include a custom helper module in all my feature tests. I have tried creating the module in spec_helper.rb, but I get the following error:

uninitialized constant FeatureHelper (NameError)

Here is my spec_helper.rb as it currently is:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
include Warden::Test::Helpers

module FeautreHelper
  def login
    shop = create(:shop)
    user = create(:user)
    login_as user, scope: :user
    user
  end
end

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.include FeatureHelper, type: :feature
...
...

( The error is from line 23 config.include FeatureHelper, type: :feature )

Why is my FeatureHelper module not being detected, and what can I do to ensure that it is?

回答1:

The module you defined does not match that which you are trying to include.

You have the module named FeautreHelper, but are trying to include FeatureHelper. Notice that there is a typo in the module name - the u is in the wrong spot.

The module should be renamed:

module FeatureHelper
  def login
    shop = create(:shop)
    user = create(:user)
    login_as user, scope: :user
    user
  end
end