RSpec 中未初始化的常量错误
uninitialized constant error in RSpec
我正在尝试使用 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 资源不包含模型。也许这与此有关?
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
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
我做错了什么?
要描述的第一个参数是该示例组的描述。您可以将 PasswordResets 放在字符串中。
describe 'PasswordResets' do
我在这里看到两个问题。
1) PasswordResets
可能不是您的寄件人的正确 class 名称。通常它会以 Mailer
结尾,类似于 PasswordResetMailer
。但如果实际上是PasswordResets
,那么我们就去2).
2) 如果您使用的是 Rails 应用,您的规范文件需要 require "rails_helper"
,而不是 spec_helper
。这将设置 Rails,并具有所有正确的自动加载功能,以找到正确的常量。这也会影响用户模型在规范中的加载,以及其他事情。
我正在尝试使用 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 资源不包含模型。也许这与此有关?
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
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
我做错了什么?
要描述的第一个参数是该示例组的描述。您可以将 PasswordResets 放在字符串中。
describe 'PasswordResets' do
我在这里看到两个问题。
1) PasswordResets
可能不是您的寄件人的正确 class 名称。通常它会以 Mailer
结尾,类似于 PasswordResetMailer
。但如果实际上是PasswordResets
,那么我们就去2).
2) 如果您使用的是 Rails 应用,您的规范文件需要 require "rails_helper"
,而不是 spec_helper
。这将设置 Rails,并具有所有正确的自动加载功能,以找到正确的常量。这也会影响用户模型在规范中的加载,以及其他事情。