RSpec中未初始化的常量错误

时间:2015-08-11 13:26:13

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

我试图使用Capybara和FactoryGirl以及RSpec来测试我的邮件。我最终得到了错误:

/spec/mailers/password_resets_spec.rb:3:in `<top (required)>': uninitialized constant PasswordResets (NameError)
    from /home/alex/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
    from /home/alex/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
    from /home/alex/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
    from /home/alex/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
    from /home/alex/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
    from /home/alex/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
    from /home/alex/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
    from /home/alex/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
    from /home/alex/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `load'
    from /home/alex/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `<main>'
    from /home/alex/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
    from /home/alex/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'

我的PasswordReset资源不包含模型。也许它与此有某种关系?

规格/邮寄者/ password_resets_spec.rb

require "spec_helper"

describe PasswordResets do
  it "emails user when requesting password reset" do
    user = FactoryGirl(:user)
    visit login_path
    click_link "Forgot your password?"
    fill_in "Email", with: user.email
    click_button "Reset Password"
  end
end

规格/ spec_helper.rb

require 'simplecov'
SimpleCov.start
require 'factory_girl_rails'
require 'capybara/rspec'

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end


  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end


end

我做错了什么?

2 个答案:

答案 0 :(得分:2)

要描述的第一个参数是该示例组的描述。您可以将PasswordResets放在一个字符串中。

describe 'PasswordResets' do

答案 1 :(得分:2)

我可以在这看到两个问题。

1)PasswordResets可能不是邮件程序的正确类名。通常它会以Mailer结尾,类似于PasswordResetMailer。但如果它实际上是PasswordResets,那么我们转到2)。

2)如果您使用的是Rails应用,则您的规范文件需要require "rails_helper",而不是spec_helper。这将设置Rails,具有所有正确的自动加载,以找到正确的常量。这也会影响规范内部用户模型的加载以及其他内容。