Cameron Booth
2008-Oct-29 15:27 UTC
[rspec-users] 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081029/eae1835e/attachment.html>
Ben Mabey
2008-Oct-29 18:45 UTC
[rspec-users] Including spec/rails/mocks into other classes
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-usersCameron, 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
Scott Taylor
2008-Oct-29 19:27 UTC
[rspec-users] Including spec/rails/mocks into other classes
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. Scott