Module: Compact

Defined in:
lib/compact.rb,
lib/compact/ledger.rb,
lib/compact/version.rb,
lib/compact/contract.rb,
lib/compact/invocation.rb,
lib/compact/verification_codes.rb,
lib/compact/argument_interceptor.rb

Overview

This TOP-level module defines the entire public API of the gem.

Defined Under Namespace

Classes: ArgumentInterceptor, Contract, Invocation, Ledger

Constant Summary collapse

VERSION =
"0.1.1"
PENDING =
:pending
VERIFIED =
:verified
FAILING =
:failing
UNTESTED =
:untested
@@ledger =
Ledger.new

Class Method Summary collapse

Class Method Details

.prepare_double(name, block = Proc.new) ⇒ Object

To record the interactions of your test_double, prepare inside a block passed to this method. Give the role played by the mock a name so we can cross-reference it with tests against the real implementation.

my_watched_mock = Compact.prepare_double('role_name') do
  mock = MyMock.new
  mock.expect(:method_name, return_args, when_called_with)
end

The returned mock is decorated with an ArgumentInterceptor that records:

  • methods sent to it

  • the arguments with which they were called

  • and the return values

and stores these Invocations in an instance of the Ledger class for comparison with the corresponding contract tests in verify_contract.



29
30
31
# File 'lib/compact.rb', line 29

def self.prepare_double(name, block = Proc.new)
  @@ledger.prepare_double(name, block)
end

.summaryObject

Unlikely to be used by end users of this gem. Used to write test reporters that give us the low-down at the end of our suite.



51
52
53
# File 'lib/compact.rb', line 51

def self.summary
  @@ledger.summary
end

.verify_contract(name, collaborator, block = Proc.new) ⇒ Object

Calling this method checks that the collaborator param is an object capable of fulfilling the role defined by name (for which see prepare_double).

Example usage:

Compact.verify_contract('role_name', myObject) do
  expected = return_value
  actual =  myObject.method_name(*args_specified_by_test_double)
  assert_equal expected, actual
end


45
46
47
# File 'lib/compact.rb', line 45

def self.verify_contract(name, collaborator, block = Proc.new )
  @@ledger.verify_contract(name, collaborator, block)
end