【发布时间】:2011-09-29 21:13:27
【问题描述】:
我正在使用 Ruby on Rails 3.1.0、rspec-rails 2 和 DelajdJob gems。为了测试用户注册时是否发送了电子邮件,我正在尝试测试延迟的电子邮件,但我遇到了一些问题\怀疑(与DelayedJob gem 没有直接关系)。
在我的控制器中,我有:
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 => 'foo@bar.com' }
# Here I would like to test
# (1) if the e-mail is send
# (2) if the e-mail is send to the 'foo@bar.com' address
end
end
如果我运行规范,我会收到以下错误:
Failure/Error: post :create,
NoMethodError:
undefined method `user_token' for nil:NilClass
我该如何解决这个问题并测试电子邮件是否已发送?我应该模拟还是存根用户类对象实例?您对我的情况有何建议?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 rspec rspec2