【问题标题】:should_receive in RSpecRSpec 中的 should_receive
【发布时间】:2010-04-21 09:30:21
【问题描述】:

据我所知,should_receive 仅适用于模拟对象。我想要检查的是某个类(不是对象)是否收到了某个消息,例如:

User.should_receive(:all).once

我该怎么做?

UPD。通常,为模型和控制器编写测试我们可以编写 User.should_receive(:smth).once。但就我而言,我正在测试 lib 文件夹中的任意类,不知何故我总是收到以下消息:

<User( [fields] ) (class)> expected :all with (no args) once, but received it 0 times>

关于为什么会这样的任何想法?测试以某种方式看到 User 类,但无法检查它是否收到消息。当然,我已经检查了十次用户 确实收到了消息。

【问题讨论】:

    标签: ruby-on-rails class rspec message expectations


    【解决方案1】:

    简单:

    User.should_receive(:all).once
    

    我想要检查的是某个类(不是对象)是否收到了某个消息

    一个类一个对象!

    【讨论】:

    • 感谢您的回答,约尔格。我已经用更多细节更新了这个问题。
    猜你喜欢
    • 2014-02-11
    • 1970-01-01
    • 2014-05-12
    • 2010-11-22
    • 1970-01-01
    • 2012-04-05
    • 2014-12-16
    • 1970-01-01
    • 2012-06-09
    相关资源
    最近更新 更多