Class: MinitestToRspec::Minitest::Stub
- Inherits:
-
Object
- Object
- MinitestToRspec::Minitest::Stub
- Defined in:
- lib/minitest_to_rspec/minitest/stub.rb
Overview
Represents an ‘expects` or a `stubs` from mocha. Conceptually the same as `Rspec::Stub`.
Instance Method Summary collapse
-
#any_instance? ⇒ Boolean
Returns true if we are stubbing any instance of ‘receiver`.
-
#count ⇒ Object
TODO: add support for - at_least - at_least_once - at_most - at_most_once - never.
-
#initialize(call) ⇒ Stub
constructor
A new instance of Stub.
-
#message ⇒ Object
Given e.g.
-
#receiver ⇒ Object
Given e.g.
- #returns ⇒ Object
- #with ⇒ Object
Constructor Details
Instance Method Details
#any_instance? ⇒ Boolean
Returns true if we are stubbing any instance of ‘receiver`.
25 26 27 28 29 |
# File 'lib/minitest_to_rspec/minitest/stub.rb', line 25 def any_instance? @call.calls_in_receiver_chain.any? { |i| i.method_name.to_s.include?('any_instance') } end |
#count ⇒ Object
TODO: add support for
-
at_least
-
at_least_once
-
at_most
-
at_most_once
-
never
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/minitest_to_rspec/minitest/stub.rb', line 65 def count case @call.method_name when :expects, :once 1 when :returns the_call_to_stubs_or_expects.method_name == :expects ? 1 : nil when :twice 2 end end |
#message ⇒ Object
Given e.g. ‘expects(:y)`, returns `:y`.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/minitest_to_rspec/minitest/stub.rb', line 32 def case @call.method_name when :expects, :stubs @call.arguments.first else call = the_call_to_stubs_or_expects if call.nil? raise UnknownVariant, 'not a mocha stub, no stubs/expects' else call.arguments.first end end end |
#receiver ⇒ Object
Given e.g. ‘X.any_instance.expects(:y)`, returns `X`.
18 19 20 21 22 |
# File 'lib/minitest_to_rspec/minitest/stub.rb', line 18 def receiver chain = @call.receiver_chain last = chain[-1] last.nil? ? chain[-2] : last end |
#returns ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/minitest_to_rspec/minitest/stub.rb', line 50 def returns case @call.method_name when :returns @call.arguments.first else @call.find_call_in_receiver_chain(:returns)&.arguments&.first end end |
#with ⇒ Object
46 47 48 |
# File 'lib/minitest_to_rspec/minitest/stub.rb', line 46 def with @call.find_call_in_receiver_chain(:with)&.arguments&.first end |