2011-09-29 64 views
1

我正在使用Ruby on Rails 3.1.0,rspec-rails 2DelajdJob宝石。为了测试用户注册时是否发送了电子邮件,我正在尝试测试延迟的电子邮件,但我遇到了一些麻烦\疑问(与DelayedJob宝石没有直接关系)。我应该如何测试邮件文件中的类对象?

在我的控制,我有:

def create 
    ... 
    Users::Mailer.delay.sign_up(@user) 
    ... 
end 

在我的邮件文件我有:

class Users::Mailer 
    def sign_up(user) 
    @user = user 

    # Note: 'authentication' refers to an associated model 
    @authentication = user.authentication.user_token 
    ... 
    end 
end 

在我的规格文件我有:

describe "POST create" do 
    it "sends e-mail" do 
    post :create, { :email => '[email protected]' } 

    # Here I would like to test 
    # (1) if the e-mail is send 
    # (2) if the e-mail is send to the '[email protected]' address 
    end 
end 

如果我运行规范我收到以下错误:

Failure/Error: post :create, 
NoMethodError: 
    undefined method `user_token' for nil:NilClass 

我该如何解决并测试电子邮件是否已发送?我应该嘲笑还是存根用户类对象实例?你对我的建议有什么建议?

回答

0
def create 
    ... # whatever is going on here is not setting @user.authentication 
    Users::Mailer.delay.sign_up(@user) 
    ... 
end 

编辑:我意识到我回答了您遇到的错误的问题。至于嘲笑用户对象,我会倾向于在这种情况下使用模拟。在这种情况下测试的重要之处在于发送了一封电子邮件,并发送至[email protected]。我会发布你需要创建一个用户然后验证。

+0

你是什么意思? – user502052

+0

这个错误“未定义的方法'user_token'为零:NilClass”意味着@ user.authentication未被设置,该代码在某处... –

+0

我知道...但我也要求提供建议那个“一般”问题\情景... – user502052

相关问题