class MinitestToRspec::Minitest::Stub

Represents an `expects` or a `stubs` from mocha. Conceptually the same as `Rspec::Stub`.

Public Class Methods

new(call) click to toggle source
# File lib/minitest_to_rspec/minitest/stub.rb, line 12
def initialize(call)
  Type.assert(Input::Model::Call, call)
  @call = call
end

Public Instance Methods

any_instance?() click to toggle source

Returns true if we are stubbing any instance of `receiver`.

# 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() click to toggle source

TODO: add support for

  • at_least

  • at_least_once

  • at_most

  • at_most_once

  • never

# 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() click to toggle source

Given e.g. `expects(:y)`, returns `:y`.

# File lib/minitest_to_rspec/minitest/stub.rb, line 32
def message
  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() click to toggle source

Given e.g. `X.any_instance.expects(:y)`, returns `X`.

# 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() click to toggle source
# 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() click to toggle source
# File lib/minitest_to_rspec/minitest/stub.rb, line 46
def with
  @call.find_call_in_receiver_chain(:with)&.arguments&.first
end

Private Instance Methods

the_call_to_stubs_or_expects() click to toggle source

Given an `exp` representing a chain of calls, like `stubs(x).returns(y).once`, finds the call to `stubs` or `expects`.

# File lib/minitest_to_rspec/minitest/stub.rb, line 80
def the_call_to_stubs_or_expects
  @call.find_call_in_receiver_chain(%i[stubs expects])
end