RSpec: Include a custom helper module in spec_helper.rb

霸气de小男生 提交于 2019-12-07 20:46:44

问题


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


来源:https://stackoverflow.com/questions/24382645/rspec-include-a-custom-helper-module-in-spec-helper-rb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!