I am trying to write a stub for a method called #alters? that takes two symbols as parameters and then returns true or false. I would like Mocha to stub #alters? only when it is called with the parameters :line and :business. When it is called with some other parameters such as :line and :carrier I would like it to not stub the method. Below is my attempt to do that. The stubbed/first call to activity.alters?(:line, :business) returns true as expected. The second call with parameters I don't want stubbed, activity.alters?(:line, :carrier), raises an unexpected invocation. Any thoughts on how to do this? Thank you for your time.
test "Foo." do
activity = Activity.new # Activity has an #alters? method on it that takes two symbols.
# First test that the #alters? method is not stubbed.
refute activity.alters?(:line, :business)
refute activity.alters?(:line, :carrier)
# Attempt to stub #alters? so that when the specific parameters :line and :business are passed in it returns true.
# Calls to other parameter combinations should still return false.
activity.stubs(:alters?).with(:line, :business).returns(true)
# Now test that it is only stubbed for the parameter combination of :line, :business
assert activity.alters?(:line, :business) # Passes.
refute activity.alters?(:line, :carrier) # Raises:
# unexpected invocation: #<Activity:0x7fc6fdda40f0>.alters?(:line, :carrier)
# satisfied expectations:
# - allowed any number of times, invoked once: #<Activity:0x7fc6fdda40f0>.alters?(:line, :business)
end