[rspec-users] Including spec/rails/mocks into other classes

Ben Mabey ben at benmabey.com
Wed Oct 29 14:45:30 EDT 2008

Cameron Booth wrote:
> 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
> ------------------------------------------------------------------------
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

I would suggest looking into 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.


More information about the rspec-users mailing list