Rspec email_spec issue

2019-01-29 09:01发布

问题:

I am going through this User Authentication tutorial..

http://larsgebhardt.de/user-authentication-with-ruby-on-rails-rspec-and-capybara/

..which uses the gem 'email_spec'. Granted the author is using an earlier version of rails and rspec, I am having issues getting the gem to work properly.

When adding..

spec_helper.rb

RSpec.configure do |config|
...
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
...
end

I receive the error..

Neither Pony nor ActionMailer appear to be loaded so email-spec is requiring ActionMailer.
WARN: Unresolved specs during Gem::Specification.reset:
      minitest (~> 5.1)
      rack-test (~> 0.6.2)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.

I can still see the expected test failures so I continued, but once I got to the part where it had me add config.include(UserHelper) to the spec_helper, the gem or test suite broke.

This side of rails and rspec is a little over my head. Any help is highly appreciated.

I am currently running Rails 4.1.6 and Rspec 3.1.7.

回答1:

There are two problems to fix here.

Firstly, there is the notice from the email_spec gem. To get rid of this, simply require action_mailer before email_spec in your rails_helper.rb:

require "action_mailer"
require "email_spec"

Actually the email_spec gem will do this for you (hence the notice), but if you want to get rid of the pesky message, this is the solution.

Secondly, there is the issue of config.include(UserHelper). This is actually unrelated to the first issue, and happens because the support/user_helper.rb file is not included when you try to reference the UserHelper module.

The reason this happens is because the article you're following was written before RSpec 3, and thus before the Rails configurations moved to the rspec_helper.rb file. (More information on the upgrade here: https://www.relishapp.com/rspec/rspec-rails/docs/upgrade)

To resolve the problem:

  1. Uncomment this line from rails_helper.rb:

    Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

    This will load all your files from the spec/support directory tree.

  2. Move all your configurations from spec_helper.rb to rails_helper.rb.