Ruby on rails 为什么这个测试不使用Pucker punch gem运行?
此代码的目的是向用户发送一封电子邮件,其中包含折扣百分比已达到给定阈值的一系列产品。产品通过以下方式退回:Ruby on rails 为什么这个测试不使用Pucker punch gem运行?,ruby-on-rails,ruby,backgroundworker,sucker-punch,Ruby On Rails,Ruby,Backgroundworker,Sucker Punch,此代码的目的是向用户发送一封电子邮件,其中包含折扣百分比已达到给定阈值的一系列产品。产品通过以下方式退回: user.notifications 它返回一个由2个元素数组组成的数组,格式如下: [[product, notification]] 通知是由折扣百分比和产品标识组成的对象 send_notification? 检查用户在过去7天内是否收到过通知,如果用户在过去一周内没有收到电子邮件,则返回布尔值true;如果用户收到了正在传入的产品,则返回布尔值false 我有以下工作和伴随
user.notifications
它返回一个由2个元素数组组成的数组,格式如下:
[[product, notification]]
通知是由折扣百分比和产品标识组成的对象
send_notification?
检查用户在过去7天内是否收到过通知,如果用户在过去一周内没有收到电子邮件,则返回布尔值true;如果用户收到了正在传入的产品,则返回布尔值false
我有以下工作和伴随的测试:
class ProductNotificationEmailJob
include SuckerPunch::Job
def perform(user)
user_notifications = user.notifications || []
products = []
notifications = []
user_notifications.each do |notification|
if notification[1].send_notification?
products << notification[0]
notifications << notification[1]
end
end
NotificationMailer.notification_email(user, products).deliver
notifications.each do |notification|
notification.update(notification_date: Time.now)
end
end
end
当我拿出电话线时:
包括suckrpunch::作业
考试通过得很好,但我无法通过那一行。出于某种原因,对于include-suckpunch::Job行,对象创建方法似乎不起作用,并且对所有值返回nil。如果我没有提供足够的细节,我会提前道歉,但我不想发布太多的代码。请留下评论,我将包括任何要求的细节。谢谢你抽出时间,我真的很感激 重新审视这个问题后,我意识到我甚至试图在ProductNotificationEmailJob类中完成所有这些工作,这违反了封装规则。我将其解压缩到另一个类中,所有内容都运行良好,完全可以测试
require 'rails_helper'
describe ProductNotificationEmailJob do
it 'performs' do
notification = ObjectCreation.create_notification
expect(notification.notification_date).to be_nil
user = notification.user
stub = double("Object")
expect(NotificationMailer).to receive(:notification_email).with(user, [notification.my_product.product]).and_return(stub)
expect(stub).to receive(:deliver)
ProductNotificationEmailJob.new.perform(user)
expect(MyProductsNotification.last.notification_date).to_not be_nil
end
end