zach.dennis at gmail.com
Tue Mar 18 23:45:39 EDT 2008
On Tue, Mar 18, 2008 at 9:10 AM, David Chelimsky <dchelimsky at gmail.com> wrote:
> Hi all,
> Over the last couple of years I've read a ton of mail from users
> concerned with false positives coming from stubbing/mocking methods
> that don't exist. I recently added a stub_model method for
> rspec_on_rails (not yet released, available in git) which I think
> mitigates this a bit. It is still in development and subject to
> change, but here's how it works now.
> It looks a lot like mock_model.
> stub_model(Person, :name => 'David')
> But it works in a fundamentally different way: FIrst, it creates a
> real instance (which means you have to create the model to use it). It
> assigns it an id by default, but you can set :id => nil if you want it
> to behave like a new record. It overrides new_record? so that it
> behaves as you would expect (false if there is an id, true if not). It
> also overrides #connection, raising an error if there is any attempt
> to access the database. This gives you the db isolation you get from
> mock_model, but with a real object.
Does this mean you have access to real model methods? If I define
FooModel#bar and I use stub_model(Foo) in a controller and someone
updates the controller to call foo#bar will it complain that an
unexpected method was called or will it call the real foo#bar method?
I hope it blows up, otherwise it acts like partial mocking classes and
that has negative drawbacks. Side rant: IMO partial mocking is evil
and should be avoided when they can be. They clutter up tests with
cases that shouldn't be there, but have to be there in order to ensure
certain calls aren't made (where a real mock would yell at you for
calling a method you didn't stub or expect). End side rant. =)
> Kind of like unit_record, but on
> an instance by instance basis.
> I've been using this for a few weeks now (just introduced it to the
> rspec code base a week or so ago) and I'm finding it very useful. I'm
> thinking of changing the generated rails specs to use stub_model
> instead of mock_model, and I'd be curious to hear your thoughts about
I'm very eager to try this out. I'm interesting to find out more as
well. I shall sync with your git repository! Thanks for your hard work
More information about the rspec-devel