Closed elvanja closed 11 years ago
By default bogus fakes instances of classes, but here you need to fake the class, So you should create fake using:
fake(:task_repository, as: :class) { Task }
Great, that solved the issue, thank you for your time and a quick answer!
A side note: expect(task_repository).to have_received.where(user_id: 100)
got me a bit confused since it is rather close to Test Spies in RSpec-Mocks. Just an observation.
Found another problem, if you don't mind helping :-)
In another test I am trying to stub Task's user_id with a value, like this: stub(task).use_id { 666 }
, and I get the following error:
Failure/Error: stub(task).user_id { stub.id { 101 } }
NameError: <Task:0x1ae24e0> does not respond to user_id
I've read the Bogus Safe Stubbing chapter, and here's what I wrote:
This time, the tasks belong to a user:
class User < ActiveRecord::Base
has_many :tasks
end
class Task < ActiveRecord::Base
belongs_to :user
end
class UpdateTask
def initialize(user, task, task_repository = Task)
...
end
end
describe UpdateTask do
fake(:user)
fake(:task)
it "requires user and task user to match" do
stub(user).id { 100 }
stub(task).user_id { 101 }
expect { described_class.new(user, task) }.to raise_error(ArgumentError)
end
end
At the same time, stubbing the User property works just fine. The tests don't touch the database at all. Any idea why this is not working?
Bogus doesn't handle methods defined on method_missing
, as it would require calling respond_to?
on the real instance. It copies only methods known at the fake creation. There is a workaround, that adds accessor methods for AR attributes based on schema,
Since the issue of stubbing fields on AR models seems tu come up pretty frequently, I added the workaround to the Bogus codebase.
Yep, my bad, forgot about the way active record defines model methods. Was hoping I'd be able to avoid hitting the database :-( Anyway, thanks for the assistance!
We've changed the mechanism for faking AR models a little. To avoid db interactions, you might consider using nulldb.
I have issue with faking active record "where" method. An example:
Executing rspec gives:
Task:0x1d65a50 does not respond to where
I am using bogus v0.0.4.