Class: Stendhal::Mocks::MockVerifier

Inherits:
Object
  • Object
show all
Defined in:
lib/stendhal/mocks/mock_verifier.rb

Defined Under Namespace

Classes: MessageExpectation

Constant Summary collapse

@@verifiers =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ MockVerifier

Returns a new instance of MockVerifier.



11
12
13
14
15
16
# File 'lib/stendhal/mocks/mock_verifier.rb', line 11

def initialize(object)
  @expectations = []
  @object = object
  @last_mocked_method = nil
  @@verifiers << self
end

Instance Attribute Details

#expectationsObject (readonly)

Returns the value of attribute expectations.



7
8
9
# File 'lib/stendhal/mocks/mock_verifier.rb', line 7

def expectations
  @expectations
end

#last_mocked_methodObject (readonly)

Returns the value of attribute last_mocked_method.



8
9
10
# File 'lib/stendhal/mocks/mock_verifier.rb', line 8

def last_mocked_method
  @last_mocked_method
end

#objectObject (readonly)

Returns the value of attribute object.



9
10
11
# File 'lib/stendhal/mocks/mock_verifier.rb', line 9

def object
  @object
end

Class Method Details

.reset!Object



58
59
60
# File 'lib/stendhal/mocks/mock_verifier.rb', line 58

def self.reset!
  @@verifiers = []
end

.verifiersObject



50
51
52
# File 'lib/stendhal/mocks/mock_verifier.rb', line 50

def self.verifiers
  @@verifiers
end

.verify!Object



54
55
56
# File 'lib/stendhal/mocks/mock_verifier.rb', line 54

def self.verify!
  verifiers.each(&:verify!)
end

Instance Method Details

#add_expectation(method, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/stendhal/mocks/mock_verifier.rb', line 22

def add_expectation(method, options = {})
  @last_mocked_method = method
  expectation_for(method).tap do |expectation|
    expectation and begin
      options[:negative] ? expectation.times_expected = 0 :
                           expectation.times_expected += 1
    end
  end || @expectations << MessageExpectation.new(method, options)
end

#add_negative_expectation(method) ⇒ Object



32
33
34
# File 'lib/stendhal/mocks/mock_verifier.rb', line 32

def add_negative_expectation(method)
  add_expectation(method, :negative => true)
end

#expectation_for(method) ⇒ Object



18
19
20
# File 'lib/stendhal/mocks/mock_verifier.rb', line 18

def expectation_for(method)
  @expectations.detect {|e| e.method == method }
end

#fulfill_expectation(method) ⇒ Object



36
37
38
# File 'lib/stendhal/mocks/mock_verifier.rb', line 36

def fulfill_expectation(method)
  @expectations.select{|e| e.method == method}.each(&:register_call)
end

#verify!Object



40
41
42
43
44
45
46
47
48
# File 'lib/stendhal/mocks/mock_verifier.rb', line 40

def verify!
  expectations.each do |expectation|
    begin
      expectation.verify
    rescue Stendhal::Exceptions::ExpectationNotMet=>e
      raise Stendhal::Exceptions::ExpectationNotMet.new "#{@object} #{e.message}"
    end
  end
end