Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 230
baash05 Avatar asked Oct 13 '14 03:10

baash05


People also ask

Is RSpec TDD or BDD?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

What is mocking in RSpec?

Mocking is a technique in test-driven development (TDD) that involves using fake dependent objects or methods in order to write a test. There are a couple of reasons why you may decide to use mock objects: As a replacement for objects that don't exist yet.

What is double in RSpec?

A Double is an object which can “stand in” for another object. You're probably wondering what that means exactly and why you'd need one. This is a simple class, it has one method list_student_names, which returns a comma delimited string of student names.


2 Answers

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.

like image 35
georgebrock Avatar answered Oct 19 '22 20:10

georgebrock


[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.

like image 75
Jared Beck Avatar answered Oct 19 '22 20:10

Jared Beck