在 RSpec 中测试更新操作,结果为真,但测试失败?
Testing an update operation in RSpec, evaluates to true, but test fails?
我有这个测试:
it "saves the notification id in the referral for future reference" do
expect { subject.perform(*args) }
.to change(referral, :notification_id).from(nil).to(customer_notification_delivery.id)
end
它 运行 上面的代码是:
if notification.present?
referral.update(friend_customer_notification_delivery_id: notification.id)
end
我添加了一些调试消息,在启动测试后检查它们,以确保满足此条件,并且代码正在 运行,我得到了 true
两者
p notification.present?
p referral.update(friend_customer_notification_delivery_id: customer_notification_delivery.id)
我遗漏了什么吗?为什么更新 returns 为真,但测试中未更新值?
我得到的输出:
expected #notification_id to have changed from nil to 5, but did not change
测试中的 referral
和被测对象中的 referral
是两个不同的对象,我敢打赌。对一个的更改不会影响另一个。 referral
在测试中不会神奇地从其他代码生成的相关数据库记录中提取更新。
我一般都是这样
it "saves the notification id in the referral for future reference" do
expect { subject.perform(*args) }
.to change{ referral.reload.notification_id }.from(nil).to(customer_notification_delivery.id)
end
我有这个测试:
it "saves the notification id in the referral for future reference" do
expect { subject.perform(*args) }
.to change(referral, :notification_id).from(nil).to(customer_notification_delivery.id)
end
它 运行 上面的代码是:
if notification.present?
referral.update(friend_customer_notification_delivery_id: notification.id)
end
我添加了一些调试消息,在启动测试后检查它们,以确保满足此条件,并且代码正在 运行,我得到了 true
两者
p notification.present?
p referral.update(friend_customer_notification_delivery_id: customer_notification_delivery.id)
我遗漏了什么吗?为什么更新 returns 为真,但测试中未更新值?
我得到的输出:
expected #notification_id to have changed from nil to 5, but did not change
referral
和被测对象中的 referral
是两个不同的对象,我敢打赌。对一个的更改不会影响另一个。 referral
在测试中不会神奇地从其他代码生成的相关数据库记录中提取更新。
我一般都是这样
it "saves the notification id in the referral for future reference" do
expect { subject.perform(*args) }
.to change{ referral.reload.notification_id }.from(nil).to(customer_notification_delivery.id)
end