我在模型中有方法
(用户模型)
def create_reset_code
self.attributes = {:reset_code => Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )}
save(:validate=>false)
UserMailer.reset_password_email(self).deliver
end
如何在 RSpec 中对其进行测试?我想测试代码生成,并发送电子邮件
PS:用谷歌,但没有找到例子
UPD
我写了两个测试:
it "should create reset code" do
@user.create_reset_code
@user.reset_code.should_not be_nil
end
it "should send reset code by email" do
@user.create_reset_code
@email_confirmation = ActionMailer::Base.deliveries.first
@email_confirmation.to.should == [@user.email]
@email_confirmation.subject.should == I18n.t('emailer.pass_reset.subject')
@email_confirmation.body.should match(/#{@user.reset_code}/)
end
但是这 --- @email_confirmation.body.should match(/#{@user.reset_code}/) ---- 不起作用
在一封信中,我给出了带有 reset_code 的 url,如下所示 reset_password_url(@user.reset_code)
固定的
it "should send reset code by email" do
@user.create_reset_code
@email_confirmation = ActionMailer::Base.deliveries.last
@email_confirmation.to.should == [@user.email]
@email_confirmation.subject.should == I18n.t('emailer.pass_reset.subject')
@email_confirmation.html_part.body.should match /#{@user.reset_code}/
end
这是工作!
谢谢大家,问题已结束