简体   繁体   English

摩卡(Mocha)Rails古怪

[英]Mocha Rails Weirdness

I'm seeing a very weird output from my Rails tests, using Mocha and Rails 3.1.0. 我在使用Mocha和Rails 3.1.0的Rails测试中看到了非常奇怪的输出。

not all expectations were satisfied
  unsatisfied expectations:
    - expected exactly once, not yet invoked: #<GitAccess:0xbb5c344>.branches(any_parameters)
  satisfied expectations:
    - allowed any number of times, invoked once: #<GitAccess:0xbb5c344>.branches(any_parameters)

It says that my "branches" method was never called, but called once - on the same object? 它说我的“分支”方法从未调用过,而是被调用过一次-在同一个对象上? How is this possible? 这怎么可能? My controller looks like this: 我的控制器如下所示:

def create
  git_access.branches()
end

I'm totally not understanding how this is possible. 我完全不知道这是怎么可能的。

Okay, here's the answer. 好的,这就是答案。 I somehow thought that .expects would only check whether the function is called or not. 我以某种方式认为.expects只会检查该函数是否被调用。 So in my test I had .expects and .stubs on the same function call, which made mocha ignore my .stubs. 因此,在测试中,我在同一函数调用上包含了.expects和.stu​​bs,这使mocha忽略了我的.stubs。

By reading a bunch of tutorials online, .stubs should be used when you want to fake the response of a method, and .expects when you want to fake the response of a method AND test whether the method is called. 通过在线阅读大量教程,当您要伪造方法的响应时应使用.stubs,而当您要伪造方法的响应时应使用.expects并测试该方法是否被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM