ruby-on-rails - RSpec:在 spec_helper.rb 中包含自定义帮助程序模块

标签 ruby-on-rails testing rspec capybara factory-bot

我正在尝试在我的所有功能测试中包含一个自定义帮助程序模块。我尝试在 spec_helper.rb 中创建模块,但出现以下错误:

uninitialized constant FeatureHelper (NameError)

这是我的 spec_helper.rb 当前的样子:

# 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
...
...

(错误来自第 23 行 config.include FeatureHelper,类型::feature )

为什么我的 FeatureHelper 模块没有被检测到,我该怎么做才能确保它被检测到?

最佳答案

您定义的模块与您尝试包含的模块不匹配。

您有名为 FeautreHelper 的模块,但正试图包含 FeatureHelper。请注意,模块名称中有一个拼写错误 - u 在错误的位置。

模块应该重命名:

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

关于ruby-on-rails - RSpec:在 spec_helper.rb 中包含自定义帮助程序模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24382645/

相关文章:

testing - 如何从覆盖率统计中排除生成的代码

java - JUnit 测试抛出异常的错误形式?

ruby-on-rails - 如何使用 rspec 为通知消息编写测试用例

ruby-on-rails - 在rails中指定gem包的非发布版本

ruby-on-rails - Rails 4 after_validation 错误消息通用 'is invalid'

javascript - Jquery Mobile - 每次在空白/白色屏幕中刷新结果

ruby-on-rails - Rails 4 和 Devise 的活跃工作

python - 如何在 py.test 中显示警告

ruby - 使用 Rspec 共享示例来测试 Ruby 类的属性

ruby - RSpec:每次指定对具有不同参数的方法的多次调用