Class: FlexMock::ExplicitNeeded

Inherits:
Object
  • Object
show all
Defined in:
lib/flexmock/explicit_needed.rb

Overview

Expectations on mocks with a base class can only be defined on methods supported by the base class. Attempting to add an stub to a method not defined on the base class will cause the expectation to be wrapped in an ExplicitNeeded wrapper. The wrapper will throw an exception unless the explicitly method is immediately called on the expectation.

Instance Method Summary collapse

Constructor Details

#initialize(expectation, method_name, base_class) ⇒ ExplicitNeeded

Returns a new instance of ExplicitNeeded.



12
13
14
15
16
17
# File 'lib/flexmock/explicit_needed.rb', line 12

def initialize(expectation, method_name, base_class)
  @expectation = expectation
  @explicit = false
  @method_name = method_name
  @base_class = base_class
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/flexmock/explicit_needed.rb', line 32

def method_missing(sym, *args, &block)
  if explicit?
    @expectation.send(sym, *args, &block)
  else
    fail NoMethodError, "Cannot stub methods not defined by the base class\n" +
      "   Method:     #{@method_name}\n" +
      "   Base Class: #{@base_class}\n" +
      "   (Use 'explicitly' to override)"
  end
end

Instance Method Details

#explicit?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/flexmock/explicit_needed.rb', line 24

def explicit?
  @explicit
end

#explicitlyObject



19
20
21
22
# File 'lib/flexmock/explicit_needed.rb', line 19

def explicitly
  @explicit = true
  self
end

#mock=(m) ⇒ Object



28
29
30
# File 'lib/flexmock/explicit_needed.rb', line 28

def mock=(m)
  @expectation.mock = m
end