Class: MacSpec::MockingFramework::MessageExpectation
- Inherits:
-
Object
- Object
- MacSpec::MockingFramework::MessageExpectation
- Defined in:
- lib/mac_spec/mocking_framework/message_expectation.rb
Instance Attribute Summary collapse
-
#msg ⇒ Object
readonly
Returns the value of attribute msg.
-
#return_value ⇒ Object
readonly
Returns the value of attribute return_value.
-
#unique_alias ⇒ Object
readonly
Returns the value of attribute unique_alias.
Class Method Summary collapse
Instance Method Summary collapse
- #and_return(value) ⇒ Object
-
#initialize(options) ⇒ MessageExpectation
constructor
A new instance of MessageExpectation.
- #negative? ⇒ Boolean
- #received_with_args!(args) ⇒ Object
- #return_value_for_args(args) ⇒ Object
- #stub? ⇒ Boolean
- #verify ⇒ Object
- #with(*args) ⇒ Object
Constructor Details
#initialize(options) ⇒ MessageExpectation
Returns a new instance of MessageExpectation.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 6 def initialize() @receiver = [:receiver] @msg = [:msg] @positive = [:positive] @stub = [:stub] @stub_return_value = [:stub_return_value] @unique_alias = "__macspec__alias_for_#{@msg}".to_sym me = self if respond_to?(@msg) && !respond_to?(@unique_alias) # todo refactor to singletonclass.instance_eval (class<<@receiver;self;end).send(:alias_method, @unique_alias, @msg) (class<<@receiver;self;end).send(:define_method, @msg) do |*args| received_args = args == [] ? nil : args me.received_with_args!(received_args) me.return_value_for_args(received_args) end elsif !respond_to?(@msg) && !respond_to?(@unique_alias) (class<<@receiver;self;end).send(:define_method, me.msg) do |*args| received_args = args == [] ? nil : args me.received_with_args!(received_args) me.return_value_for_args(received_args) end else # end MessageExpectation.register_for_verification(@receiver) @return_value = {:__macspec_anyargs => true} @received = {} @args_expectation = :__macspec_anyargs end |
Instance Attribute Details
#msg ⇒ Object (readonly)
Returns the value of attribute msg.
5 6 7 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 5 def msg @msg end |
#return_value ⇒ Object (readonly)
Returns the value of attribute return_value.
5 6 7 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 5 def return_value @return_value end |
#unique_alias ⇒ Object (readonly)
Returns the value of attribute unique_alias.
5 6 7 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 5 def unique_alias @unique_alias end |
Class Method Details
.register ⇒ Object
43 44 45 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 43 def self.register @register end |
.register_for_verification(obj) ⇒ Object
39 40 41 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 39 def self.register_for_verification(obj) @register << obj end |
.verify ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 47 def self.verify begin @register.uniq.each do |obj| obj && obj.__macspec__verify end ensure @register = [] end end |
Instance Method Details
#and_return(value) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 120 def and_return(value) unless stub? || negative? @return_value[@args_expectation] = value self end end |
#negative? ⇒ Boolean
90 91 92 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 90 def negative? !@positive end |
#received_with_args!(args) ⇒ Object
94 95 96 97 98 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 94 def received_with_args!(args) @received[args] = true unless args == nil @received[:__macspec_anyargs] = true @return_value[:__macspec_anyargs] ||= true end |
#return_value_for_args(args) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 100 def return_value_for_args(args) return @stub_return_value if stub? args ||= :__macspec_anyargs received_args = @return_value.keys unless received_args.include?(args) || received_args.include?(:__macspec_anyargs) args_string = args == :__macspec_anyargs ? "No Args" : args err_msg = "Wrong Argument(s): '#{args_string}' For message '#{msg}'. Receiver: '#{@receiver}'" MacSpec.flunk err_msg end @return_value[args] end |
#stub? ⇒ Boolean
86 87 88 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 86 def stub? @stub end |
#verify ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 57 def verify unless self.stub? @return_value.each do |args,value| args_string = (args == :__macspec_anyargs) ? "Any Args" : args.inspect if (@received[args] && @positive) || (!@received[args] && !@positive) MacSpec.assert(true) elsif (!@received[args] && @positive) received_with_args_string = if !@received.keys.include?(:__macspec_anyargs) "not at all." else @received.delete(:__macspec_anyargs) "#{@received.keys}" end err_msg = """ '#{@receiver}' should have received '#{msg}' with '#{args_string}'. But received it #{received_with_args_string} """ MacSpec.flunk err_msg elsif (@received[args] && !@positive) err_msg = "'#{@receiver}' should *not* have received '#{msg}' with '#{args_string}'." MacSpec.flunk err_msg end end end @return_value = {} end |
#with(*args) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/mac_spec/mocking_framework/message_expectation.rb', line 112 def with(*args) unless stub? @args_expectation = args @return_value[@args_expectation] = nil self end end |