1
我正在使用Ruby on Rails 3.1.0,rspec-rails 2
和DelajdJob
宝石。为了测试用户注册时是否发送了电子邮件,我正在尝试测试延迟的电子邮件,但我遇到了一些麻烦\疑问(与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
我该如何解决并测试电子邮件是否已发送?我应该嘲笑还是存根用户类对象实例?你对我的建议有什么建议?
你是什么意思? – user502052
这个错误“未定义的方法'user_token'为零:NilClass”意味着@ user.authentication未被设置,该代码在某处... –
我知道...但我也要求提供建议那个“一般”问题\情景... – user502052