简体   繁体   中英

uninitialized constant error in RSpec

I'm trying to test my mailer using Capybara and FactoryGirl along with 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. Maybe it's somehow related with this?

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

What am I doing wrong?

The first argument to describe is the description of that example group. You can put PasswordResets in a string.

describe 'PasswordResets' do

I can see two problems here.

1) PasswordResets might not be the correct class name of your mailer. Typically it would end in Mailer and be something like PasswordResetMailer . But if it is actually PasswordResets , then we go to 2).

2) If you're in a Rails app, your spec files need to require "rails_helper" , not spec_helper . This will set up Rails, with all the right autoloading, to find the right constants. This will also affect the loading of the User model inside the spec, and other things as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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