gpt4 book ai didi

ruby-on-rails - RSpec 测试模型和邮件程序

转载 作者:行者123 更新时间:2023-12-02 19:41:14 27 4
gpt4 key购买 nike

我在模型中有方法

(用户模型)

  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:使用Google,但没有找到示例

更新

我编写了两个测试:

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

开始工作了!

谢谢大家,问题已结束

最佳答案

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.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

关于ruby-on-rails - RSpec 测试模型和邮件程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11106599/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com