2011-09-04 88 views
2

我是Rspec,Capybara和Rails的新手。我正在嘗試使用Rails 3.1運行基本郵件測試。我基本上是遵循Railscast#275,其中Ryan引用了一個url助手,因爲我在這裏除了他之外,它工作。在Rails中訪問URL助手RSpec郵件程序測試

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 

但它似乎並不像我使用的網址助手。給定的錯誤是:

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

我剛剛在我的一個郵件程序上運行了你的測試,它工作正常。請你可以創建一個帶有錯誤的示例應用程序,並將其放在github上,我會看看。 – Gazler

+0

當然。無論如何,它實際上是開源的。 https://github.com/TimFletcher/OneListRails –

+0

所以我實際設置並運行Ryan的Railscast#275,測試通過的很好。這似乎是我的項目特有的東西。 –

回答

1

爲了Rspec自動加載URL助手,您需要確保包含您的郵件程序的文件夾被稱爲mailers而不是mailer