2014-10-10 87 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