2014-10-10 88 views
1

Rails 4.1.4和Rspec 3ActionMailer在測試郵件時失敗

我正在用Rspec做一個非常基本的電子郵件測試。如果我從Rails控制檯調用郵件程序,它可以很好地工作。如果我把它從郵件規範我得到:

wrong number of arguments (0 for 1..2) 

的郵件是很基本的:

def create_team_invite(org, email) 
    @organization = org 
    mail(:to=>email, :subject=>'Test Subject') 
    end 

測試是非常基本的太:

it 'can send out emails to invite others to create teams' do 
    UserMailer.create_team_invite(@org, '[email protected]').deliver 
    expect(ActionMailer::Base.deliveries.count).to eq 1 
    mail = ActionMailer::Base.deliveries.first 
    expect(mail.subject).to eq 'Test Subject' 
    expect(mail.from).to eq '[email protected]' 
end 

它在失敗的「 mail(:to ...「)在我的環境中,似乎可能是它的一些配置問題,但我的測試設置與Dev完全一樣,使用SMTP並將它發送到Mailcatcher端口。 looke d在Backtrace,但沒有看到任何異常...

以前有人看過這個嗎?

更新:提供請求的其他信息。

我test.rb,減去評論:

Rails.application.configure do 
    config.cache_classes = true 
    config.eager_load = false 
    config.serve_static_assets = true 
    config.static_cache_control = 'public, max-age=3600' 
    config.consider_all_requests_local  = true 
    config.action_controller.perform_caching = false 
    config.action_dispatch.show_exceptions = false 
    config.action_controller.allow_forgery_protection = false 
    config.action_mailer.delivery_method = :smtp 
    config.action_mailer.perform_deliveries = true 
    config.active_support.deprecation = :stderr 
    config.action_mailer.default_url_options = { :host => 'lvh.me:3000', :only_path=>false } 
end 

整個rspec的失敗是:

UserMailer 

團隊加入電子郵件 可以發送電子郵件邀請他人創建團隊(失敗 - 1 )

失敗:

1) UserMailer Team Joining Email can send out emails to invite others to create teams 
    Failure/Error: UserMailer.create_team_invite(@team, '[email protected]').deliver 
    ArgumentError: 
     wrong number of arguments (0 for 1..2) 
    # ./app/mailers/user_mailer.rb:11:in `create_team_invite' 
    # ./spec/mailers/user_mailer_spec.rb:36:in `block (3 levels) in <top (required)>' 

Finished in 0.25858 seconds (files took 29 minutes 48 seconds to load) 
1 example, 1 failure 

我配置電子郵件的方式是通過一個初始化程序,從每個環境的配置中加載一個email.yml文件。測試和開發人員使用完全相同的過程,具有相同的設置。 (同樣,我發送給Mailcatcher,而不是僅僅以mail_delivery:測試)

更新2

我已經把範圍縮小到梅勒缺少「請求」對象。如果我挖通,其中錯誤發生(一個AbstractController rendering.rb,線109)它試圖引用請求對象:

if defined?(request) && request && request.variant.present? 

這被調用到機架test.rb線121:

def request(uri, env = {}, &block) 
    env = env_for(uri, env) 
    process_request(uri, env, &block) 
    end 

因此,它像Rack Test.rb類被視爲該抽象控制器中的請求方法...但我不知道如何,或爲什麼,或爲什麼會發生在這個特定的項目中...

+3

請分享您的'environments/test.rb'。另外,我們可以看到整個測試失敗... – Anthony 2014-10-10 14:54:25

+0

我上面分享了他們 - 不要以爲你有任何想法? – 2014-10-18 14:26:46

+0

我刪除了我的答案。我認爲這是模板中的URI問題,但似乎沒有。我只是隱藏了錯誤,所以我認爲它工作。仍然得到相同的錯誤,並追蹤它在寶石中沒有顯示任何有用的東西。美好時光。 – 2014-10-18 15:19:38

回答

0

I得到這個相同的確切的錯誤,似乎它試圖調用機架測試任務代碼,當我只是試圖測試郵件對象。所以我所做的只是不包括我爲郵件規範在spec_helper中放置的所有東西。這解決了我的問題。

# require 'spec_helper' -> I removed this line, 
# and manually require the files that's needed to just test the mailer object: 
require 'rubygems' 
require './config/environment' 
require './app/mailers/your_mailer' 

注:我只是在使用action_mailer做一個機架項目,沒有rails的東西。所以你的解決方案會有所不同,但你明白了。

更新:

做一些更多的故障排除。我發現這個問題,在我的spec_helper.rb文件

RSpec.configure do |config| 
    config.include include RackSpecHelper # notice the double include 
    ... 
end 

# where RackSpecHelper is a custom module 
module RackSpecHelper 
    include Rack::Test::Methods 
    # a bunch of other helper methods 
end 

通知的雙重包括在這條線

config.include include RackSpecHelper 

起初,我嘗試了刪除線,我的郵件測試運行就好了。可疑與雙有,我刪除了包括所以它只是像這樣

config.include RackSpecHelper 

現在我的郵件測試運行良好,而不必做手工需要像我張貼早些時候以上(並且它可以與其他測試一起運行使用機架測試的東西)。

雙重包括,基本上是做

config.include(include(RackSpecHelper)) 

,其中包括在配置塊RackSpecHelper,它加載在頂級命名空間中的所有方法! (非常糟糕的事情)。它可以工作,但這意味着Rack :: Test :: Methods的所有方法都位於全局名稱空間中。

所以我猜你的情況,你可能在spec_helper一條線,包括機架::測試::在這樣

include Rack::Test::Methods 

全局命名空間的方法刪除它,而是把它放在像這樣的RSpec配置

RSpec.configure do |config| 
    config.include RackSpecHelper 
end