Contents

50. Customizing arguments and return values

As a developer I want the ability to make contract arguments and return values more or less specific

Background:

  • Given the following classes:
    ruby
    class ObjectUnderTest
      def do_something(collaborator, input)
        collaborator.some_method(input)
      end
    end
    
    class Collaborator
      def some_method(input)
      end
    end
  • And the test suite is configured to use VerifiedDouble:
    ruby
    require 'verified_double'
    require 'verified_double/rspec_configuration'
    require 'main'
  • And a test that uses VerifiedDouble to mock an object:
    ruby
    require 'spec_helper'
    describe ObjectUnderTest do
      let(:input) { :input }
      let(:output) { :output }
      let(:instance_double) { VerifiedDouble.of_instance('Collaborator') }
    
      it "tests something" do
        expect(instance_double).to receive(:some_method).with(input).and_return(output)
        ObjectUnderTest.new.do_something(instance_double, input)
      end
    end

Scenario: More general argument

  • Given the test suite has a contract test for the mock:
    ruby
    require 'spec_helper'
    
    describe 'Collaborator' do
      it "tests something", verifies_contract: 'Collaborator#some_method(Object)=>Symbol' do
        # do nothing
      end
    end
  • When I run the test suite
  • Then I should not see any output saying the mock is unverified

Scenario: More specific argument

  • Given the test suite has a contract test for the mock:
    ruby
    require 'spec_helper'
    
    describe 'Collaborator' do
      it "tests something", verifies_contract: 'Collaborator#some_method(:input)=>Symbol' do
        # do nothing
      end
    end
  • When I run the test suite
  • Then I should not see any output saying the mock is unverified

Scenario: More general return value

  • Given the test suite has a contract test for the mock:
    ruby
    require 'spec_helper'
    
    describe 'Collaborator' do
      it "tests something", verifies_contract: 'Collaborator#some_method(Symbol)=>Object' do
        # do nothing
      end
    end
  • When I run the test suite
  • Then I should not see any output saying the mock is unverified

Scenario: More specific return value

  • Given the test suite has a contract test for the mock:
    ruby
    require 'spec_helper'
    
    describe 'Collaborator' do
      it "tests something", verifies_contract: 'Collaborator#some_method(Symbol)=>:output' do
        # do nothing
      end
    end
  • When I run the test suite
  • Then I should not see any output saying the mock is unverified

Scenario: class double as return value

  • Given a test that uses VerifiedDouble to mock an object:
    ruby
    require 'spec_helper'
    describe ObjectUnderTest do
      let(:collaborator_class){ VerifiedDouble.of_class('Collaborator') }
      let(:instance_double) { VerifiedDouble.of_instance('Collaborator', class: collaborator_class) }
    
      it "tests something" do
        expect(instance_double.class).to eq(collaborator_class)
      end
    end
  • And the test suite has a contract test for the mock:
    ruby
    require 'spec_helper'
    
    describe 'Collaborator' do
      it "tests something", verifies_contract: 'Collaborator#class()=>Class' do
        # do nothing
      end
    end
  • When I run the test suite
  • Then I should not see any output saying the mock is unverified

Source: features/customizing_arguments_and_return_values.feature at commit ce9b1c52, Verified Double (MIT license).