Including spec/rails/mocks into other classes

Hi all,

So my potentially crazy but I think good idea right now is to take the
factory_girl gem and adapt it to generate stubbed models instead of
actual
ActiveRecord objects that get saved to the DB. My main reasoning there
is
that I like the syntax they’ve set up and some of the tools under the
hood
to deal with associations, etc, but I want to be disconnected from the
database.

However, for mocking my ActiveRecord objects right now (in a rails
project)
I’m using mock_model from rspec-rails a lot, and I would love to use
that in
this code too. But I’m hitting a few walls including the methods into a
separate class. So far I’ve got this:

class Factory

include Spec::Mocks
include Spec::Mocks::Methods
include Spec::Rails::Mocks

def create_stubbed(attrs = {})
model = mock_model(build_class)
end

I’m getting a method missing on the method “stub” however, so I feel
like
somehow I’m not including the right stuff, or maybe I should be
extending
some things, not quite sure. (And yes, that’s the method “stub”, not
“stub!”)

Any advice would be awesome, thanks!

Cameron

Cameron B. wrote:

project) I’m using mock_model from rspec-rails a lot, and I would love
model = mock_model(build_class)
Cameron


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Cameron,
I would suggest looking into nulldb:
http://agilewebdevelopment.com/plugins/nulldb

With it I am able to maintain my same model factory interface without
ever hitting the DB. Using the adapter is nice because you can turn it
on or off when you please so you can really hit the DB in the cases that
you need to.

HTH,
Ben

On Oct 29, 2008, at 11:27 AM, Cameron B. wrote:

project) I’m using mock_model from rspec-rails a lot, and I would
model = mock_model(build_class)
end

I’m getting a method missing on the method “stub” however, so I feel
like somehow I’m not including the right stuff, or maybe I should be
extending some things, not quite sure. (And yes, that’s the method
“stub”, not “stub!”)

Any advice would be awesome, thanks!

Are you requiring “spec/mocks” or “spec/mocks/mock” (I forget which
one it is, at the moment).

Try doing it through irb, requiring the appropriate spec/mocks*.rb
file, and including the module.

Scott