Ronze
Ronze

Reputation: 1574

Mocha Rails Weirdness

I'm seeing a very weird output from my Rails tests, using Mocha and Rails 3.1.0.

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.

Upvotes: 0

Views: 138

Answers (1)

Ronze
Ronze

Reputation: 1574

Okay, here's the answer. I somehow thought that .expects would only check whether the function is called or not. So in my test I had .expects and .stubs on the same function call, which made mocha ignore my .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.

Upvotes: 1

Related Questions