Class: MinitestToRspec::Model::Calls::Returns
- Inherits:
-
MinitestToRspec::Model::Call
- Object
- Base
- MinitestToRspec::Model::Call
- MinitestToRspec::Model::Calls::Returns
- Defined in:
- lib/minitest_to_rspec/model/calls/returns.rb
Overview
Represents a call to ‘returns`, the stubbing method from `mocha`.
Constant Summary collapse
- KNOWN_RECEIVERS =
%i[stubs expects with].freeze
Instance Attribute Summary
Attributes inherited from MinitestToRspec::Model::Call
Instance Method Summary collapse
- #any_instance? ⇒ Boolean
- #expectation? ⇒ Boolean
-
#initialize(exp) ⇒ Returns
constructor
A new instance of Returns.
- #known_variant? ⇒ Boolean
- #message ⇒ Object
-
#rspec_mocks_method ⇒ Object
To avoid a ‘ProcessingError` please check `known_variant?` before calling `rspec_mocks_method`.
-
#rspec_msg_recipient ⇒ Object
Returns ‘Sexp` representing the object that will receive the stubbed message.
- #stub? ⇒ Boolean
-
#values ⇒ Object
The return values.
- #with ⇒ Object
Methods inherited from MinitestToRspec::Model::Call
#argument_types, #arguments, #assert_difference?, assert_difference?, #assert_no_difference?, assert_no_difference?, assert_nothing_raised?, #assert_nothing_raised?, assert_raise?, #assert_raise?, assert_raises?, #assert_raises?, #calls_in_receiver_chain, #method_name, method_name?, #num_arguments, #one_string_argument?, #question_mark_method?, #raise_error_args?, #receiver, #receiver_call, #receiver_chain, #receiver_chain_include?, #require_test_helper?
Methods included from SexpAssertions
#assert_sexp_type, #assert_sexp_type_array, #sexp_type?
Constructor Details
#initialize(exp) ⇒ Returns
Returns a new instance of Returns.
14 15 16 17 |
# File 'lib/minitest_to_rspec/model/calls/returns.rb', line 14 def initialize(exp) @exp = exp raise UnknownVariant unless known_variant? end |
Instance Method Details
#any_instance? ⇒ Boolean
23 24 25 |
# File 'lib/minitest_to_rspec/model/calls/returns.rb', line 23 def any_instance? receiver_chain_include?(:any_instance) end |
#expectation? ⇒ Boolean
27 28 29 |
# File 'lib/minitest_to_rspec/model/calls/returns.rb', line 27 def expectation? receiver_chain_include?(:expects) end |
#known_variant? ⇒ Boolean
31 32 33 34 35 36 37 38 |
# File 'lib/minitest_to_rspec/model/calls/returns.rb', line 31 def known_variant? r = receiver !r.nil? && r.sexp_type == :call && KNOWN_RECEIVERS.include?(Call.new(r).method_name) && !values.empty? && .sexp_type == :lit end |
#message ⇒ Object
40 41 42 43 44 |
# File 'lib/minitest_to_rspec/model/calls/returns.rb', line 40 def calls_in_receiver_chain .find { |c| [:stubs, :expects].include? c.method_name } .arguments[0] end |
#rspec_mocks_method ⇒ Object
To avoid a ‘ProcessingError` please check `known_variant?` before calling `rspec_mocks_method`.
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/minitest_to_rspec/model/calls/returns.rb', line 48 def rspec_mocks_method s = stub? e = expectation? if s && e raise ProcessingError, "Method chain contains stubs and expects" elsif s :allow elsif e :expect else raise ProcessingError, "Found returns without stubs or expects" end end |
#rspec_msg_recipient ⇒ Object
Returns ‘Sexp` representing the object that will receive the stubbed message. Examples:
banana.stubs(:delicious?).returns(true)
Kiwi.any_instance.stubs(:delicious?).returns(false)
On the first line, the ‘rspec_msg_recipient` is `banana`. On the second, `Kiwi`.
71 72 73 |
# File 'lib/minitest_to_rspec/model/calls/returns.rb', line 71 def rspec_msg_recipient receiver_chain.compact.last end |
#stub? ⇒ Boolean
19 20 21 |
# File 'lib/minitest_to_rspec/model/calls/returns.rb', line 19 def stub? receiver_chain_include?(:stubs) end |
#values ⇒ Object
The return values
76 77 78 |
# File 'lib/minitest_to_rspec/model/calls/returns.rb', line 76 def values arguments end |
#with ⇒ Object
80 81 82 83 |
# File 'lib/minitest_to_rspec/model/calls/returns.rb', line 80 def with w = calls_in_receiver_chain.find { |c| c.method_name == :with } w.nil? ? [] : w.arguments end |