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

发布于 2024-12-03 00:23:33 字数 3384 浏览 0 评论 0原文

我是 Rspec、Capybara 和 Rails 的新手。我正在尝试使用 Rails 3.1 运行基本的邮件测试。我基本上遵循 Railscast #275,其中 Ryan 引用了一个 url 帮助器,就像我在这里所做的那样,除了他之外,它有效。

spec/mailer/user_mailer_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

相关的路线如下所示:

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"}

如果有任何用处,我的spec/spec_helper.rb看起来像这样:

# 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" }

I'm new to Rspec, Capybara and Rails. I'm trying to run a basic mailer test using Rails 3.1. I'm basically following Railscast #275 where Ryan references a url helper as I'm doing here except for him, it works.

spec/mailer/user_mailer_spec.rb looks like:

describe UserMailer do
  it 'should have access to URL helpers' do
    lambda { password_resets_path }.should_not raise_error
  end
end

But it doesn't seem to like the URL helpers I'm using. The given error is:

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

The pertinent routes look like this:

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"}

If it's of any use, my spec/spec_helper.rb looks like this:

# 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

I also have this line in my config/environments/test.rb

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羞稚 2024-12-10 00:23:33

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

In order for Rspec to autoload the URL helpers, you need to ensure the folder containing your mailer is called mailers rather than mailer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文