Ruby on rails 在Rails RSpec Mailer测试中访问URL帮助程序

Ruby on rails 在Rails RSpec Mailer测试中访问URL帮助程序,ruby-on-rails,rspec,ruby-on-rails-3.1,capybara,Ruby On Rails,Rspec,Ruby On Rails 3.1,Capybara,我是Rspec、水豚和Rails的新手。我正在尝试使用Rails3.1运行一个基本的mailer测试。我基本上遵循Railscast#275,Ryan在这里引用url帮助程序,除了他之外,它是有效的 spec/mailer/user\u mailer\u spec.rb看起来像: describe UserMailer do it 'should have access to URL helpers' do lambda { password_resets_path }.should

我是Rspec、水豚和Rails的新手。我正在尝试使用Rails3.1运行一个基本的mailer测试。我基本上遵循Railscast#275,Ryan在这里引用url帮助程序,除了他之外,它是有效的

spec/mailer/user\u mailer\u spec.rb
看起来像:

describe UserMailer do
  it 'should have access to URL helpers' do
    lambda { password_resets_path }.should_not raise_error
  end
end
但它似乎不喜欢我使用的URL帮助程序。给定的错误是:

Timothys-MacBook-Pro:onelist-rails-3.1 tim$ rspec spec
F....

Failures:

  1) UserMailer should have access to URL helpers
     Failure/Error: lambda { password_resets_path }.should_not raise_error
       expected no Exception, got #<NameError: undefined local variable or method `password_resets_path' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fa004697390>>
     # ./spec/mailer/user_mailer_spec.rb:19:in `block (2 levels) in <top (required)>'

Finished in 1.31 seconds
5 examples, 1 failure

Failed examples:

rspec ./spec/mailer/user_mailer_spec.rb:18 # UserMailer should have access to URL helpers
如果有任何用处,我的
spec/spec\u helper.rb
如下所示:

Timothys-MacBook-Pro:onelist-rails-3.1 tim$ rake routes | grep password
    password_resets GET    /password_resets(.:format)                {:action=>"index", :controller=>"password_resets"}
                    POST   /password_resets(.:format)                {:action=>"create", :controller=>"password_resets"}
 new_password_reset GET    /password_resets/new(.:format)            {:action=>"new", :controller=>"password_resets"}
edit_password_reset GET    /password_resets/:id/edit(.:format)       {:action=>"edit", :controller=>"password_resets"}
     password_reset GET    /password_resets/:id(.:format)            {:action=>"show", :controller=>"password_resets"}
                    PUT    /password_resets/:id(.:format)            {:action=>"update", :controller=>"password_resets"}
                    DELETE /password_resets/:id(.:format)            {:action=>"destroy", :controller=>"password_resets"}
# This file is copied to spec/ when you run 'rails generate rspec:install'           
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # == Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  config.mock_with :rspec

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  # config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  config.include(MailerMacros)
  config.before(:each) { reset_email }
end
我的
config/environments/test.rb中也有这一行

# Set URLs to work properly in email
config.action_mailer.default_url_options = { :host => "www.example.com" }

为了让Rspec自动加载URL帮助程序,您需要确保包含邮件的文件夹名为
mailers
,而不是
mailer

,我刚刚在我的一个邮件上运行了您的测试,它工作正常。请您创建一个带有错误的示例应用程序,并将其放在github上,我来看看。当然。无论如何,目前它实际上是开源的。因此,我实际上设置并运行了Ryan的Railscast 275,测试顺利通过。这似乎与我的项目有关。好的,我已经解决了。包含my mailer测试的文件夹名为
mailer
,而不是
mailers
。等我同意的时候,我会把这当作一个恰当的回答。