简体   繁体   English

RSpec中未初始化的常量错误

[英]uninitialized constant error in RSpec

I'm trying to test my mailer using Capybara and FactoryGirl along with RSpec. 我正在尝试使用Capybara和FactoryGirl以及RSpec来测试我的邮件程序。 I ended up having the error: 我最终遇到了错误:

/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>'

My PasswordReset resource doesn't contain a model. 我的PasswordReset资源不包含模型。 Maybe it's somehow related with this? 也许与此有某种联系?

spec/mailers/password_resets_spec.rb spec / mailers / 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/spec_helper.rb spec / 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

What am I doing wrong? 我究竟做错了什么?

The first argument to describe is the description of that example group. 要描述的第一个参数是该示例组的描述。 You can put PasswordResets in a string. 您可以将PasswordResets放在字符串中。

describe 'PasswordResets' do

I can see two problems here. 我在这里看到两个问题。

1) PasswordResets might not be the correct class name of your mailer. 1) PasswordResets可能不是您邮件程序的正确类别名称。 Typically it would end in Mailer and be something like PasswordResetMailer . 通常它将以Mailer结尾,并且类似于PasswordResetMailer But if it is actually PasswordResets , then we go to 2). 但是,如果实际上是PasswordResets ,则转到2)。

2) If you're in a Rails app, your spec files need to require "rails_helper" , not spec_helper . 2)如果您在Rails应用中,则您的spec文件需要使用require "rails_helper" ,而不是spec_helper This will set up Rails, with all the right autoloading, to find the right constants. 这将设置具有正确的自动加载功能的Rails,以查找正确的常量。 This will also affect the loading of the User model inside the spec, and other things as well. 这也会影响规范中User模型的加载,以及其他方面的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM