thebenman
thebenman

Reputation: 1621

Using instance_of parameter matcher in mocha

I want to use a instance_of parameter matcher for a test in ActionController::TestCase. I have imported the following files

require 'mocha/api'
require 'mocha/setup'

I wish to write something like this.

Book.any_instance.stubs(:read).with(instance_of(String)).returns true

But I get a message no method error for instance_of. Although I am able to use it in another file where it is configured for RSpec. Could you please let me know the required files I need to import to use the parameter matchers

Upvotes: 0

Views: 501

Answers (1)

Masafumi Okura
Masafumi Okura

Reputation: 724

https://github.com/freerange/mocha#rspec says we should use config.mock_with :mocha in RSpec config where you find working.

Let's dive into the code on GitHub. The code for mock_with is found here.

It seems it requires another file and it's here.

You can check the code above and will see that you need to require mocha like this:

begin
  require 'mocha/api'

  begin
    require 'mocha/object'
  rescue LoadError
    # Mocha >= 0.13.0 no longer contains this file nor needs it to be loaded.
  end
rescue LoadError
  require 'mocha/standalone'
  require 'mocha/object'
end

Note that you need not to require exactly like this. When you know the version of mocha you use, you can require required components only.

Upvotes: 0

Related Questions