gpt4 book ai didi

ruby-on-rails - rspec 接收方法未按预期工作

转载 作者:数据小太阳 更新时间:2023-10-29 07:20:50 27 4
gpt4 key购买 nike

我无法理解为什么在运行我的测试时,以下代码在 rspec 中不起作用。

Lead.update_status(2,2,true)
expect(Lead).to receive(:update_status).with(2, 2, true)

我也试过

Lead.update_status(2,2,true)
expect_any_instance_of(Lead).to receive(:update_status).with(2, 2, true)

我得到的错误是

(<Lead(id: integer, contact_id: integer, course_presentation_id: integer, status: integer, created_by_user_id: integer, updated_by_user_id: integer, created_at: datetime, updated_at: datetime, mailchimp_status: integer) (class)>).update_status(2, 2, true)
expected: 1 time with arguments: (2, 2, true)
received: 0 times with arguments: (2, 2, true)

请注意,如果我将“测试”添加到我的代码中,则会打印出来,所以我知道 update_status 实际上在工作。

有什么想法吗?谢谢。

最佳答案

您应该将您的期望放在您希望调用该方法的代码之前:

expect(Lead).to receive(:update_status).with(2, 2, true)
Lead.update_status(2,2,true)

关于ruby-on-rails - rspec 接收方法未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22531466/

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