baash05
baash05

Reputation: 4516

In rspec can we turn off verify_partial_doubles for one test

My project has this value set in the rspec_helper.rb file

mocks.verify_partial_doubles = true  

I have a test that is getting flagged

TaskPublisher does not implement #publish

The problem is that the method doesn't exist on the object until it's instantiated. It's a module import based on the the type of task to be published. (meta programming)

So I'm wondering if there is a way to turn off verify_partial_doubles for a specific test, but not affect the other tests that do have the value.

Side question: Doesn't having this flag set to true make BDD impossible? It seems to me it flies in the face of Mocking as it's defined(https://stackoverflow.com/tags/mocking/info).

Upvotes: 19

Views: 2442

Answers (2)

Jared Beck
Jared Beck

Reputation: 17538

[Is there] a way to turn off [verify_partial_doubles] for a specific test .. ?

RSpec >= 3.6

Use without_partial_double_verification

it 'example' do
  without_partial_double_verification do
    # ...
  end
end

http://rspec.info/blog/2017/05/rspec-3-6-has-been-released/

RSpec < 3.6

Yes, with user-defined metadata and a global "around hook":

# In your spec ..
describe "foo", verify_stubs: false do
  # ...
end

# In spec_helper.rb
RSpec.configure do |config|
  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.around(:each, verify_stubs: false) do |ex|
    config.mock_with :rspec do |mocks|
      mocks.verify_partial_doubles = false
      ex.run
      mocks.verify_partial_doubles = true
    end
  end
end

I believe credit for this technique goes to Nicholas Rutherford, from his post in rspec-rails issue #1076.

Upvotes: 28

georgebrock
georgebrock

Reputation: 30163

We encountered a similar problem recently, and ended up going with this:

config.mock_with :rspec do |mocks|
  mocks.verify_partial_doubles = true

  config.around(:example, :without_verify_partial_doubles) do |example|
    mocks.verify_partial_doubles = false
    example.call
    mocks.verify_partial_doubles = true
  end
end

Very similar to Jared Beck's answer, but avoiding a second call to mock_with.

Upvotes: 4

Related Questions