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

Scott Taylor mailing_lists at railsnewbie.com
Wed Oct 29 15:27:26 EDT 2008

On Oct 29, 2008, at 11:27 AM, 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!

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.


More information about the rspec-users mailing list