【问题标题】:rspec receive method not working as expectedrspec接收方法没有按预期工作
【发布时间】:2014-03-20 11:24:08
【问题描述】:

我不明白为什么在运行我的测试时以下代码在 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)

请注意,如果我将 puts "test" 添加到我的代码中,则会打印出来,所以我知道 update_status 确实有效。

有什么想法吗?谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby rspec ruby-on-rails-4


    【解决方案1】:

    你应该把你的期望放在你期望调用该方法的代码之前:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多