Class: MiniSpec::Mocks::Validations
- Inherits:
-
Object
- Object
- MiniSpec::Mocks::Validations
- Includes:
- Utils
- Defined in:
- lib/minispec/mocks/validations.rb,
lib/minispec/mocks/validations/order.rb,
lib/minispec/mocks/validations/raise.rb,
lib/minispec/mocks/validations/throw.rb,
lib/minispec/mocks/validations/yield.rb,
lib/minispec/mocks/validations/amount.rb,
lib/minispec/mocks/validations/caller.rb,
lib/minispec/mocks/validations/return.rb,
lib/minispec/mocks/validations/arguments.rb
Defined Under Namespace
Classes: AnyYield
Instance Method Summary collapse
-
#and_raise(*expected, &block) ⇒ Object
(also: #and_raised, #and_raised?)
expect received message(s) to raise a exception.
-
#and_return(*expected, &block) ⇒ Object
(also: #and_returned)
extending expectation by expecting a specific returned value.
-
#and_throw(*expected, &block) ⇒ Object
(also: #and_thrown, #and_thrown?)
checks whether received message throws expected symbol.
-
#and_yield(*expected, &block) ⇒ Object
(also: #and_yielded, #and_yielded?)
extending expectation by expecting received message to yield.
-
#count(*expected, &block) ⇒ Object
(also: #times)
assure expected message(s) was received a specific amount of times.
-
#initialize(base, object, context, *expected_messages) ⇒ Validations
constructor
A new instance of Validations.
- #once ⇒ Object
-
#ordered(n = 1, &block) ⇒ Object
checks whether expected messages was received in a specific order.
- #twice ⇒ Object
-
#with(*expected, &block) ⇒ Object
validates received arguments against expected ones.
- #with_caller(*expected, &block) ⇒ Object
- #without_arguments ⇒ Object (also: #without_any_arguments)
-
#without_raise ⇒ Object
make sure received message(s) does not raise any exception.
-
#without_throw ⇒ Object
assure received message does not throw a symbol.
-
#without_yield ⇒ Object
make sure received message wont yield.
Methods included from Utils
#any_match?, #array_elements_map, #catch_symbol, #exception_raised?, #extract_thrown_symbol, #match?, #method_visibility, #pp, #rejected?, #shorten_source, #source, #symbol_thrown?, #undefine_method, #valid_proxy_arguments?, #zipper
Constructor Details
#initialize(base, object, context, *expected_messages) ⇒ Validations
Returns a new instance of Validations.
6 7 8 9 10 11 12 13 |
# File 'lib/minispec/mocks/validations.rb', line 6 def initialize base, object, context, * .empty? && raise(ArgumentError, 'Wrong number of arguments (3 for 4+)') .all? {|m| m.is_a?(Symbol)} || raise(ArgumentError, 'Only symbols accepted') @base, @object, @context, @failed = base, object, context, false @expected_messages = .freeze @messages = expected_and_received.freeze end |
Instance Method Details
#and_raise(*expected, &block) ⇒ Object Also known as: and_raised, and_raised?
expect received message(s) to raise a exception.
if no args given any raised exception accepted. if a class given it checks whether raised exception is of given type. if a string or regexp given it checks whether raised message matches it.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/minispec/mocks/validations/raise.rb', line 24 def and_raise *expected, &block return self if @failed # `and_raise` can be called without arguments expected.empty? || (*expected, &block) received = raised_exceptions if block return @base.instance_exec(*received.values, &block) || exception_error!(@expected_messages, block, received) end expected = ? {@expected_messages[0] => expected} : zipper(@expected_messages, expected) context = @context.merge(negation: nil, right_proc: nil) # do NOT alter @context received.each_pair do |msg,calls| # each message should raise as expected at least once calls.any? {|c| exception_raised?(c, context, *expected[msg]) == true} || exception_error!(msg, expected[msg], msg => calls) end self end |
#and_return(*expected, &block) ⇒ Object Also known as: and_returned
extending expectation by expecting a specific returned value
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/minispec/mocks/validations/return.rb', line 17 def and_return *expected, &block return self if @failed (*expected, &block) received = returned_values if block return @base.instance_exec(*received.values, &block) || returned_value_error!(@expected_messages, block, received) end expected = zipper(@expected_messages, expected) received.each_pair do |msg,values| # each message should return expected value at least once values.any? {|v| validate_returned_value(expected[msg], v)} || returned_value_error!(msg, expected[msg], msg => values) end self end |
#and_throw(*expected, &block) ⇒ Object Also known as: and_thrown, and_thrown?
you can match against thrown symbol but not against value. this is a WONTFIX limitation. though it is doable this would introduce a new layer of unproven complexity.
checks whether received message throws expected symbol
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/minispec/mocks/validations/throw.rb', line 15 def and_throw *expected, &block return self if @failed expected.all? {|x| x.is_a?(Symbol)} || raise(ArgumentError, '`and_throw` accepts only symbols') # `and_throw` can be called without arguments expected.empty? || (*expected, &block) received = thrown_symbols if block return @base.instance_exec(*received.values, &block) || throw_error!(@expected_messages, block, received) end expected = zipper(@expected_messages, expected) received.each_pair do |msg,calls| # each message should throw expected symbol at least once. # if no specific symbol expected, check whether any symbol thrown. calls.any? {|s| expected[msg] ? s == expected[msg] : s.is_a?(Symbol)} || throw_error!(msg, expected[msg], msg => calls) end self end |
#and_yield(*expected, &block) ⇒ Object Also known as: and_yielded, and_yielded?
extending expectation by expecting received message to yield
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/minispec/mocks/validations/yield.rb', line 43 def and_yield *expected, &block return self if @failed # `and_yield` can be called without arguments expected.empty? || (*expected, &block) received = yielded_values if block return @base.instance_exec(*received.values, &block) || yield_error!(@expected_messages, block, received) end ? validate_yields(expected, received) : validate_yields_list(expected, received) self end |
#count(*expected, &block) ⇒ Object Also known as: times
assure expected message(s) was received a specific amount of times
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/minispec/mocks/validations/amount.rb', line 20 def count *expected, &block return self if @failed (*expected, &block) received = received_amounts if block return @base.instance_exec(*received.values, &block) || amount_error!(@expected_messages, block, received) end expected = zipper(@expected_messages, expected) received.each_pair do |,amount| # each message should be received expected amount of times amount == expected[] || amount_error!(, expected[], amount) end self end |
#once ⇒ Object
40 |
# File 'lib/minispec/mocks/validations/amount.rb', line 40 def once; count(1); end |
#ordered(n = 1, &block) ⇒ Object
this method will work only when multiple messages expected. that’s it, unlike RSpec, it wont work like this: ‘expect(obj).to_receive(:a).ordered` `expect(obj).to_receive(:b).ordered`
instead it will work like this: ‘expect(obj).to_receive(:a, :b).ordered`
checks whether expected messages was received in a specific order
20 21 22 23 24 25 |
# File 'lib/minispec/mocks/validations/order.rb', line 20 def ordered n = 1, &block block && raise(ArgumentError, '#ordered does not accept a block') n.is_a?(Integer) || raise(ArgumentError, '#ordered expects a single Integer argument') && raise(ArgumentError, '#ordered works only with multiple messages') received_in_expected_order?(n) end |
#twice ⇒ Object
41 |
# File 'lib/minispec/mocks/validations/amount.rb', line 41 def twice; count(2); end |
#with(*expected, &block) ⇒ Object
validates received arguments against expected ones
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/minispec/mocks/validations/arguments.rb', line 75 def with *expected, &block return self if @failed (*expected, &block) received = received_arguments if block return @base.instance_exec(*received.values, &block) || arguments_error!(@expected_messages, block, received) end ? validate_arguments(expected, received) : validate_arguments_list(expected, received) self end |
#with_caller(*expected, &block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/minispec/mocks/validations/caller.rb', line 3 def with_caller *expected, &block return self if @failed (*expected, &block) received = received_callers if block return @base.instance_exec(*received.values, &block) || caller_error!(@expected_messages, block) end expected = zipper(@expected_messages, expected) received.each_pair do |msg,callers| # each message should be called from expected caller at least once callers.any? {|line| caller_match?(line, expected[msg])} || caller_error!(msg, expected[msg]) end self end |
#without_arguments ⇒ Object Also known as: without_any_arguments
91 92 93 94 95 96 97 98 |
# File 'lib/minispec/mocks/validations/arguments.rb', line 91 def without_arguments return self if @failed received_arguments.each_pair do |msg,args| # each message should be called without arguments at least once args.any?(&:empty?) || arguments_error!(msg, [], msg => args) end self end |
#without_raise ⇒ Object
make sure received message(s) does not raise any exception
54 55 56 57 58 59 60 |
# File 'lib/minispec/mocks/validations/raise.rb', line 54 def without_raise return self if @failed raised_exceptions.each_pair do |msg,calls| calls.any? {|r| r.is_a?(Exception)} && unexpected_exception_error!(msg, calls) end self end |
#without_throw ⇒ Object
assure received message does not throw a symbol
44 45 46 47 48 49 50 |
# File 'lib/minispec/mocks/validations/throw.rb', line 44 def without_throw return self if @failed thrown_symbols.each_pair do |msg,calls| calls.any? {|x| x.is_a?(Symbol)} && unexpected_throw_error!(msg, calls) end self end |
#without_yield ⇒ Object
make sure received message wont yield
67 68 69 70 71 72 73 74 |
# File 'lib/minispec/mocks/validations/yield.rb', line 67 def without_yield return self if @failed yielded_values.each_pair do |msg,calls| next if calls.all?(&:nil?) unexpected_yield_error!(msg, calls) end self end |