Discussion:
stub! and stubs
Sam Woodard
2009-10-13 00:37:55 UTC
Permalink
I have an interesting setup: I am using rspec for mocking but I have
mocha installed which give me access to any_instance, expects, etc. The
problem that I am having is that I want to stub out a method for the
duration of a single example, throughout that example but only for that
example.

If I do,

Goal.any_instance.stub!(:valid?).and_return(true)

this is not sufficient because valid is called more than once

If I do,

Goal.any_instance.stubs(:valid?).returns(true) then my tests are not
independent but dependent on one another.

Can anyone help?

Thanks in advance,
Sam
--
Posted via http://www.ruby-forum.com/.
David Chelimsky
2009-10-13 01:14:37 UTC
Permalink
Post by Sam Woodard
I have an interesting setup: I am using rspec for mocking but I have
mocha installed which give me access to any_instance, expects, etc.
The
problem that I am having is that I want to stub out a method for the
duration of a single example, throughout that example but only for that
example.
If I do,
Goal.any_instance.stub!(:valid?).and_return(true)
this is not sufficient because valid is called more than once
If I do,
Goal.any_instance.stubs(:valid?).returns(true) then my tests are not
independent but dependent on one another.
Can anyone help?
While mocha and rspec's mocks might be compatible for you in some
cases, it is purely accidental. I'd strongly recommend you choose one
or the other. If any_instance is important to you, I'd choose mocha.
If not, choose whichever makes you happier.

Cheers,
David
Post by Sam Woodard
Thanks in advance,
Sam
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
http://rubyforge.org/mailman/listinfo/rspec-users
Scott Taylor
2009-10-13 01:26:38 UTC
Permalink
Post by David Chelimsky
Post by Sam Woodard
I have an interesting setup: I am using rspec for mocking but I have
mocha installed which give me access to any_instance, expects, etc. The
problem that I am having is that I want to stub out a method for the
duration of a single example, throughout that example but only for that
example.
If I do,
Goal.any_instance.stub!(:valid?).and_return(true)
this is not sufficient because valid is called more than once
If I do,
Goal.any_instance.stubs(:valid?).returns(true) then my tests are not
independent but dependent on one another.
Can anyone help?
While mocha and rspec's mocks might be compatible for you in some
cases, it is purely accidental. I'd strongly recommend you choose
one or the other. If any_instance is important to you, I'd choose
mocha. If not, choose whichever makes you happier.
Unless I'm incorrect, this is still a feature request on the
lighthouse tracker.

I don't see why we couldn't use something like this code:

http://github.com/smtlaissezfaire/mix_master

to implement the mixin/mixout feature that we'd need to support
different frameworks per-example group.

Scott
Post by David Chelimsky
Cheers,
David
Post by Sam Woodard
Thanks in advance,
Sam
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
http://rubyforge.org/mailman/listinfo/rspec-users
_______________________________________________
rspec-users mailing list
http://rubyforge.org/mailman/listinfo/rspec-users
David Chelimsky
2009-10-13 01:41:10 UTC
Permalink
Post by Scott Taylor
Post by David Chelimsky
Post by Sam Woodard
I have an interesting setup: I am using rspec for mocking but I have
mocha installed which give me access to any_instance, expects, etc. The
problem that I am having is that I want to stub out a method for the
duration of a single example, throughout that example but only for that
example.
If I do,
Goal.any_instance.stub!(:valid?).and_return(true)
this is not sufficient because valid is called more than once
If I do,
Goal.any_instance.stubs(:valid?).returns(true) then my tests are not
independent but dependent on one another.
Can anyone help?
While mocha and rspec's mocks might be compatible for you in some
cases, it is purely accidental. I'd strongly recommend you choose
one or the other. If any_instance is important to you, I'd choose
mocha. If not, choose whichever makes you happier.
Unless I'm incorrect, this is still a feature request on the
lighthouse tracker.
http://github.com/smtlaissezfaire/mix_master
to implement the mixin/mixout feature that we'd need to support
different frameworks per-example group.
Hey Scott - I haven't looked into mixmaster yet, but supporting
multiple mock frameworks is definitely something I want to do/support
in rspec-2. Just been busy trying to get the book done and on paper
before I focus on the next version of rspec.

I think this is a separate issue, however. Sam is looking to mix
frameworks in a single statement.

FWIW,
David
Post by Scott Taylor
Scott
Post by David Chelimsky
Cheers,
David
Post by Sam Woodard
Thanks in advance,
Sam
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
http://rubyforge.org/mailman/listinfo/rspec-users
_______________________________________________
rspec-users mailing list
http://rubyforge.org/mailman/listinfo/rspec-users
_______________________________________________
rspec-users mailing list
http://rubyforge.org/mailman/listinfo/rspec-users
Sam Woodard
2009-10-13 14:01:09 UTC
Permalink
Things seem to be working better now that I am using just Mocha,
however, I am still having problems with
Foo.any_instance.stubs(:valid?).returns(false). I later tests, valid is
still stubbed out to be false. How to I stub something for exactly the
duration of one example?

Thanks,
Sam
--
Posted via http://www.ruby-forum.com/.
Lee Hambley
2009-10-13 14:33:44 UTC
Permalink
Sam,

As the .any_instance call suggests, anything ever again for the duration of
the test will stub that value, the alternative is this:

Foo.any_instance.stubs(:valid?).returns(false)

replace with this

foo = Foo.new
foo.stubs(:valid?).returns(false)

HTH

- Lee
Pat Maddox
2009-10-21 08:51:43 UTC
Permalink
I have an interesting setup:  I am using rspec for mocking but I have
mocha installed which give me access to any_instance, expects, etc.  The
problem that I am having is that I want to stub out a method for the
duration of a single example, throughout that example but only for that
example.
If I do,
Goal.any_instance.stub!(:valid?).and_return(true)
this is not sufficient because valid is called more than once
If I do,
Goal.any_instance.stubs(:valid?).returns(true) then my tests are not
independent but dependent on one another.
Can anyone help?
While mocha and rspec's mocks might be compatible for you in some cases,
it is purely accidental. I'd strongly recommend you choose one or the other.
If any_instance is important to you, I'd choose mocha. If not, choose
whichever makes you happier.
Unless I'm incorrect, this is still a feature request on the lighthouse
tracker.
http://github.com/smtlaissezfaire/mix_master
to implement the mixin/mixout feature that we'd need to support different
frameworks per-example group.
David and I began work in an experimental branch that would make
any_instance easy without any mixin magic (although admittedly it does
do const redefinition magic ;) I should finish that up at some point.

Pat

Loading...