Stubbing find in ActiveRecord considering only the first argument

For some unknown (yet) reason, an association is trying to call
Model.find(1, conditions: nil) instead of Model.find(1), which doesn’t
allow me to write “Model.stub!(:find).with(1).and_returns(@model)”.

Instead of trying to figure out (I will anyway) why it is been calling
this way, what I really wanted was to be able to stub ‘find’ whether it
is called find(id) or find(id, options) in a single line, since I don’t
want to give extra focus on this because this is not the subject of my
tests. They should be read cleanly.

Is it possible? I know I can do this:

Model.stub!(:find){|a, *args| a==1 ? s : nil }

But it is too ugly… :slight_smile:

Also, can someone point me some more in-depth documentation about mocks
and stubs in Rspec? The Rdoc and the official documentation seem to be
far from complete… For instance, from the documentation I’m unable to
find any references to mock(MyModel), for instance. So I’m not sure what
exactly this does… For instance, if I create 2 instances, will each of
the mocks have different ID’s? I can get some hints by experimentation,
but this is not documented anywhere… For instance, calling
“mock(Model).id” will raise a not existent method error. So I tested
“mock(Model, id: 1)” and it worked.

Then I have the following dilemma. Using Machinist with Blueprint, Sham
and Faker made fixture generation a trivial task while making my tests
pretty clean. On the other side, they are a bit slow… I don’t know if
Machinist can be integrated to some mock framework or if I can get about
the same result using mocks instead of real instances. I took a glance
over FactoryGirl and it seems to have some kind of support but couldn’t
find any in-depth documentation for it either…

Suppose we have in Rails:

class ModelA
belongs_to :model_b

I would like to have some kind of factory with some syntax like:

a=factory.create(ModelA) # or ModelA.make, like Machinist does… == 1 == 1

b=factory.create(ModelA) == 2 == 2

c=factory.create(ModelA, model_b: b.model_b) == 3 == 2

I really don’t care if it will be 1, 2, etc as long as they are
unique… a, b, c should all be mocked versions of the class. Is there
already any implementation of this idea?

Was I clear enough? Sometimes I have a feeling I’m not being clear
enough and this is one of these times :slight_smile:

Best regards, Rodrigo.

I’ve just found mock_model:

Seems like it solves my problem :wink:

Em 20-04-2011 22:34, Rodrigo Rosenfeld R. escreveu: