Ruby on rails 无法测试rspec rails中调用的邮件程序操作
在rails的mailer中,我知道所有的方法都是类方法。 但我无法测试我的mailer方法,该方法名为: 用户\u邮件\u spec.rb:Ruby on rails 无法测试rspec rails中调用的邮件程序操作,ruby-on-rails,rspec,mailer,Ruby On Rails,Rspec,Mailer,在rails的mailer中,我知道所有的方法都是类方法。 但我无法测试我的mailer方法,该方法名为: 用户\u邮件\u spec.rb: it "should call send_notifition method" do @user = FactoryGirl.build(:user) notify_email = double(:send_notifition) expect(UsersMailer.new).to receive(:no
it "should call send_notifition method" do
@user = FactoryGirl.build(:user)
notify_email = double(:send_notifition)
expect(UsersMailer.new).to receive(:notify_email).with(@user)
@user.save
end
用户_mailer.rb:
def notify(user)
mail to: user.email, subject: "Example"
end
user.rb:
after_commit :send_notifition
private
def send_notifition
UsersMailer.notify(self)
end
上述代码将不会通过,但当我将通知更改为自我通知时,它将通过:
def self.notify(user)
mail to: user.email, subject: "Example"
end
首先,我想为您指出一个测试电子邮件的绝妙宝石: 我认为问题在于,您正在断言
UsersMailer.new
,因此在不同的实例上放置一个mock,而不是由User
模型实例化的实例。我通常会这样测试电子邮件,没有任何问题:
it "should call send_notifition method" do
@user = FactoryGirl.build(:user)
mail = double(:mail)
expect(UsersMailer).to receive(:notify_email).with(@user).and_return(mail)
expect(mail).to receive(:deliver_later) # or deliver_now, if you don't use a background queue
@user.save
end
请注意我是如何做的expect(UsersMailer)
而不是expect(UsersMailer.new)
,同时也不要认为我是在断言电子邮件已实际送达(我认为您的代码中缺少一个deliver语句)
希望这能有所帮助。首先,我想为您指出一个测试电子邮件的绝妙宝石: 我认为问题在于,您正在断言
UsersMailer.new
,因此在不同的实例上放置一个mock,而不是由User
模型实例化的实例。我通常会这样测试电子邮件,没有任何问题:
it "should call send_notifition method" do
@user = FactoryGirl.build(:user)
mail = double(:mail)
expect(UsersMailer).to receive(:notify_email).with(@user).and_return(mail)
expect(mail).to receive(:deliver_later) # or deliver_now, if you don't use a background queue
@user.save
end
请注意我是如何做的expect(UsersMailer)
而不是expect(UsersMailer.new)
,同时也不要认为我是在断言电子邮件已实际送达(我认为您的代码中缺少一个deliver语句)
希望有帮助。解决了:
感谢@Clemens Kofler的支持。
我的代码中有很多错误:
- 第一:无需安装gem“email_spec”,并更改user.rb文件
- 第二:更改用户\u mailer\u spec.rb文件
- 最后:测试环境的config/environments/test.rb中的config mailer可以使用mailer(因为规范在测试环境中运行)
- 第一:无需安装gem“email_spec”,并更改user.rb文件
- 第二:更改用户\u mailer\u spec.rb文件
- 最后:测试环境的config/environments/test.rb中的config mailer可以使用mailer(因为规范在测试环境中运行)
UsersMailer.notify(self).deliver\u now
(或者deliver\u later
,如果您想使用后台工作程序,或者如果您使用的是较旧的Rails版本,则只需deliver
)。对不起,我写错了。我使用“expect(UsersMailer).来接收(:notify_email).与(@user)”一起使用,但它仍然不起作用。你是否像我提到的那样修复了呼叫?也就是说,回调应该是UsersMailer.notify(self).deliver\u now
(或者deliver\u later
如果您想使用后台工作程序,或者如果您使用的是旧版本的Rails,则只需deliver
)。
after_commit :send_notifition
private
def send_notifition
UsersMailer.notify(self).deliver
end
it "should call send_notifition method" do
@user = FactoryGirl.build(:user)
expect(@user).to receive(:send_notifition)
notify_email = double(:send_notifition)
expect(UsersMailer.new).to receive(:notify_email).with(@user)
@user.save
end
it "should call send_notifition_mail_if_created_new_hospital method" do
@user = FactoryGirl.build(:user)
# I don't know why "expect(@user).to receive(:send_notifition)" not passed here
mail = double(:mail)
expect(UsersMailer).to receive(:notify_email).with(@user).and_return(mail)
allow(mail).to receive(:deliver)
@user.save
end