【问题标题】:Set an expectation without mocking anything在不嘲笑任何事情的情况下设定期望
【发布时间】:2012-05-08 16:18:01
【问题描述】:

使用 MiniTest::Spec 和 Mocha:

 describe "#devices" do
   it "scopes the devices by the provided :ip_list" do
     ips = 'fast tests ftw!'
     ds = DeviceSearch.new ip_list: ips
     Device.expects(:scope_by_ip_list).once.with(ips)
     ds.devices
   end
 end

当我使代码正常工作时,此测试将失败,因为调用 Device.expects(:scope_by_ip_list)stubs Device.scope_by_ip_list,并且由于我没有指定 .returns(Devices.scoped) 或类似的,它用nil 存根方法。所以,在我的代码中,它正确地定义了一个设备列表,然后进行了进一步的操作,进一步的操作就会崩溃。

我不想想要指定一个.returns 参数,因为我完全不在乎它返回什么。我根本不想存根该方法!我只是想对它设定一个期望,让它按原样运行。

有没有办法做到这一点?

(对我来说,说Device.expects(:foo).returns('bar') 之类的话似乎很尴尬——当我说Model 期望 method,我不是存根那个方法!如果我们想存根它,我们可以说Device.stubs(:foo)。)

【问题讨论】:

    标签: ruby-on-rails stub mocha.js expectations


    【解决方案1】:

    该行为是有意的,无法更改。查看以下帖子,了解如何绕过它:

    rspec 2: detect call to method but still have it perform its function

    【讨论】:

    • 感谢您提供有趣的链接!但是,我正在使用 Minitest 并使用 Mocha 进行存根/期望,因此 RSpec 示例在很大程度上无关紧要。
    • 不幸的是,我忽略了这一点。很抱歉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2014-06-23
    • 1970-01-01
    • 2014-10-27
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多