[rspec-users] stub calls to an instance method before it is created?

David Chelimsky dchelimsky at gmail.com
Sat Nov 3 01:38:50 EDT 2007


On Nov 2, 2007 2:36 AM, Shane Wolf <shane.wolf at zvents.com> wrote:
> I am desperately in need of functionality such as Mocha offers with their
> any_instance method.  I see that there is an open ticket for a similar
> feature in RSpec, but it does not look like anything have been done. Is
> there any other way around this?
>
> https://rubyforge.org/tracker/index.php?func=detail&aid=6791&group_id=797&atid=3152
>
>  Here is what I am doing, and why I feel I need this (maybe my design is
> inherently wrong)
>
> Given a model called Foo
>
> def Foo.create_from_form
>   foo = Foo.new
>   # Do all kinds of crazy stuff here
>   foo.save
> end
>
> In my specs I want to stub the foo.save call to return true or false
> depending on the case, so that it doesn't actually try and save to the DB...
> is there any way around this since I do not actually know the instance of
> the class I am trying to stub?

foo = mock("foo")
Foo.stub!(:new).and_return(foo)
foo.should_receive(:all_kinds_of_crazy_stuff)
Foo.create_from_form(arg, arg, arg).should equal(foo)


More information about the rspec-users mailing list