Closed simaoneves closed 2 years ago
The current spec implementation requires that the stub signature matches the original method. It effectively replaces it, and if it doesn't match, then the stub doesn't work. Clunky and not the greatest, I plan to rework the stub system.
This works for me, can you confirm?
mock Dog do
stub pet(times = 3)
end
The current spec implementation requires that the stub signature matches the original method. It effectively replaces it, and if it doesn't match, then the stub doesn't work. Clunky and not the greatest, I plan to rework the stub system.
This works for me, can you confirm?
mock Dog do stub pet(times = 3) end
Wasn't aware that default arguments is effectively a new method signature, then it makes sense. Your suggestions fixes the example, but only for when you want to use the value in the default argument. Here is another example that uses a custom value and it fails:
require "spectator"
class Person
def initialize(@dog = Dog.new)
end
def pet
@dog.pet
end
def pet_more
@dog.pet(5)
end
end
class Dog
def initialize()
end
def pet(times = 2)
"woof" * times
end
end
Spectator.describe Person do
mock Dog do
stub pet(times = 2)
end
describe "#pet" do
it "pets the persons dog" do
dog = Dog.new
person = Person.new(dog)
allow(dog).to receive(pet()).and_return("woof")
result = person.pet
expect(dog).to have_received(pet()).with(2)
end
end
describe "#pet_more" do
it "pets the persons dog alot" do
dog = Dog.new
person = Person.new(dog)
allow(dog).to receive(pet()).and_return("woof")
result = person.pet_more
expect(dog).to have_received(pet()).with(5)
end
end
end
Thanks for looking into this 😄
This issue should be resolved in v0.11.0. There is a spec dedicated to it. Please reopen if your issue is not resolved.
It seems that default arguments are not working properly when the method in question is being stubbed.
The error is:
Adding another
stub
to the mock (something likestub pet()
) also does not work.