Class: Handoff::Assertion

Inherits:
Object
  • Object
show all
Defined in:
lib/handoff/assertion.rb

Overview

This is where the fluent stuff is. Get an assertion by calling Handoff#assert_handoff. Specify the delegating behavior with

  • from or from_any,

  • to,

  • for, for_method or for_methods, and

  • (optionally) with or with_arguments.

Instance Method Summary collapse

Constructor Details

#initialize(mock_delegate) ⇒ Assertion

Returns a new instance of Assertion.



20
21
22
# File 'lib/handoff/assertion.rb', line 20

def initialize mock_delegate
  @mock_delegate = mock_delegate
end

Instance Method Details

#conductObject

:stopdoc:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/handoff/assertion.rb', line 74

def conduct
  validate_sufficiently_specified
  @delegator.expects(@delegate_accessor).at_least_once.returns(@mock_delegate)
  expected_returns = {}
  method_map.each_pair do |delegating_method, target|
    expected_returns[delegating_method] = "#{delegating_method.to_s}_return"
    expectation = @mock_delegate.expects(target).returns(expected_returns[delegating_method])
    expectation.with(*@args) unless @args.nil?
  end
  method_map.each_pair do |delegating_method, target|
    send_args = [delegating_method, @args].flatten
    actual = delegator.send(*send_args)
    unless actual.equal? expected_returns[delegating_method]
      raise "expected #{delegating_method.to_s} to return #{expected_returns[delegating_method]}, but was #{actual}"
    end
  end
end

#for_methods(*args) ⇒ Object Also known as: for_method, for

Tells the assertion what delegating methods to test and what methods in the delegate they should delegate to.

:call-seq: for_methods(:delegating_method1, :delegating_method2, …)

for_methods({:delegating_method1 => :target_method1, :delegating_method2 => :target_method2})
args

symbols representing the delegating methods if they are not renamed, or a Hash mapping methods in the delegating class to methods in the delegate.



53
54
55
56
# File 'lib/handoff/assertion.rb', line 53

def for_methods(*args)
  @method_map = args.extend(ArgumentNormalizer).to_method_map
  self
end

#from(delegator) ⇒ Object

Specifies the object being tested, returning self.



33
34
35
36
# File 'lib/handoff/assertion.rb', line 33

def from delegator
  @delegator = delegator
  self
end

#from_any(class_under_test) ⇒ Object

Creates an instance of class_under_test with no arguments and uses it as the object under test.



28
29
30
# File 'lib/handoff/assertion.rb', line 28

def from_any class_under_test
  from(class_under_test.new)
end

#to(delegate_accessor) ⇒ Object

Specifies the accessor the object under test will use to acquire its delegate, returning self.



40
41
42
43
# File 'lib/handoff/assertion.rb', line 40

def to(delegate_accessor)
  @delegate_accessor = delegate_accessor
  self
end

#with_arguments(*args) ⇒ Object Also known as: with



60
61
62
# File 'lib/handoff/assertion.rb', line 60

def with_arguments *args
  @args = args
end

#with_no_argumentsObject

Specifies that the assertion should pass no arguments to the delegating method and require that no arguments are sent to the target method. Equivalent to calling with_arguments with no arguments.



68
69
70
# File 'lib/handoff/assertion.rb', line 68

def with_no_arguments
  with_arguments
end