xaviershay / rspec-fire

Obsolete - use verifying doubles in RSpec 3
MIT License
361 stars 16 forks source link

Arity on stubbed methods #29

Closed sporto closed 10 years ago

sporto commented 11 years ago

If I do

class Foo
    def run(amount)
    end
end

f = fire_double('Foo')
f.stub(run: true)
expect(f.run).to be_true

test passes, but I passed the wrong number of arguments to the method. It would be great if somehow this could be prevented.

xaviershay commented 11 years ago

I thought it already did this :(

Maybe using expect syntax doesn't go through same code path as should_receive? I'll have to poke at it.

myronmarston commented 11 years ago

It has nothing to do with the syntax used. (Actually, we haven't yet add an expect based syntax to rspec-mocks, but plan to). It has to do with the fact that rspec-fire checks arity when you constrain a mocked or stubbed method using with. rspec-fire doesn't get involved at method invocation time; that's all rspec-mocks at that point, and when you stub a method using the hash syntax (e.g. f.stub(run: true)) it doesn't constrain the number of arguments at all.

sporto commented 11 years ago

@myronmarston right

If I do

f.stub(:run).with(1).and_return(true)

It does check the number of arguments.

Any chance it could check even without the with?

myronmarston commented 11 years ago

I think that should be possible if rspec-fire converted a call like f.stub(run: true) into:

arity = f.method(:run).arity
f.stub(:run).with([anything] * arity).and_return(true)

(Of course, there's more to it than that to handle splats, default args, etc, but you get the point).

xaviershay commented 10 years ago

We addressed this in RSpec 3.