Daniel Tsadok
Daniel Tsadok

Reputation: 573

Mocha: Silence satisfied expectations

Very often when I have a missed expectation in a unit test using mocha, it spits out dozens or hundreds of "satisfied expectations" that I really don't care about. There's so much of it that I have to redirect testing output to a temp file, which is really annoying.

I am using Rails 2.3 and Mocha 0.10.0.

To clarify, I have the same issue as in Mocha Mock Carries To Another Test , and the solution there has not worked for me. However, even if I can get that resolved, I would like to suppress "satisfied expectations".

Thanks.

Upvotes: 1

Views: 206

Answers (1)

Shadwell
Shadwell

Reputation: 34774

You could monkey patch mocha to achieve this. There's a method on Mocha::Mockery that returns the satisfied expectations which you could patch to return an empty array:

module Mocha
  class Mockery
    def satisfied_expectations
      []
    end
  end
end

If you put this in test_helper.rb it'll get picked up.

Alternatively for a bit more flexibility you could opt to only hide them when an environment variable is set:

module Mocha
  class Mockery
    def satisfied_expectations_with_optional
      if ENV['MOCHA_HIDE_SATISFIED']
        []
      else
        satisfied_expectations_without_optional
      end
    end
    alias_method_chain :satisfied_expectations, :optional
  end
end

Then run your tests like this:

> MOCHA_HIDE_SATISFIED=1 rake test

Upvotes: 1

Related Questions