John Bachir
John Bachir

Reputation: 22731

Asking rspec to stub find(1), instead it's encountering find(1, {:conditions=>nil})

I'm telling rspec:

@foo1 = Factory(:foo)
@foo2 = Factory(:foo)
Foo.stub(:find).with(@foo1.id){@foo1}
Foo.stub(:find).with(@foo2.id){@foo2}

As a spec for this code:

f = Foo.find(foo_id)

And I get this error:

expected: (1)
got: (1, {:conditions=>nil})

I thought perhaps stub can't be told what parameters to expect, and I have to just use should_receive, even though that's not the behavior I'm testing in this spec -- but I tried that and it has the same error.

Upvotes: 2

Views: 3371

Answers (1)

Mark Thomas
Mark Thomas

Reputation: 37517

I think you're missing something. The stub just returns a canned response so you don't actually have to retrieve data from the database. You still need an expectation on your own code.

#In some spec...
@foo = mock(Foo)
Foo.stub!(:find).and_return(@foo)
#...do stuff that calls Foo.find...
x.should be_y

Of course, you can always use fixtures to seed your test database, then you don't have to mock ActiveRecord at all...

Upvotes: 3

Related Questions