Hey folks, How do I mock a model that is given to me by a Rails plugin? I''m trying to mock Session from the restful_authentication plugin but I get a number of errors telling me RSpec doesn''t recognize Session. NameError in ''SessionsController handling GET /sessions/new should be successful'' uninitialized constant Session An example of my usage: before do @session = mock_model(Session) Session.stub!(:new).and_return(@session) end Thanks! Dan Croak http://thoughtbot.com organic brains. digital solutions. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071018/14456927/attachment-0001.html
On 10/18/07, Dan Croak <dcroak at thoughtbot.com> wrote:> > Hey folks, > > How do I mock a model that is given to me by a Rails plugin? I''m trying to > mock Session from the restful_authentication plugin but I get a number of > errors telling me RSpec doesn''t recognize Session. > > NameError in ''SessionsController handling GET /sessions/new should be > successful'' > uninitialized constant Session > > An example of my usage: > > before do > @session = mock_model(Session) > Session.stub!(:new).and_return(@session) > end > > Thanks! > > Dan Croak > http://thoughtbot.com > organic brains. digital solutions. > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >class Session; end You can stick that in spec_helper.rb if you use Session in a lot of places. I think you can also do Session.class_eval {} somewhere and that''ll load up the actual Session class instead of the dummy implementation I just showed. It won''t use the implementation in your specs of course, but it loads the provided class into memory. Pat
Keith McDonnell
2007-Oct-18 23:31 UTC
[rspec-users] Mocking models provided in Rails plugins
Hi Dan, You might find these links helpful when spec''ing restful auth http://www.vaporbase.com/postings/Rspec_1.0_and_Restful_Authentication http://jonathan.tron.name/articles/2007/01/02/rspec-on-rails-restful-authentication http://www.caboo.se/articles/2006/10/29/rspec-on-rails Keith
Brandon Keepers
2007-Oct-18 23:38 UTC
[rspec-users] Mocking models provided in Rails plugins
Dan, On Oct 18, 2007, at 6:14 PM, Dan Croak wrote:> Hey folks, > > How do I mock a model that is given to me by a Rails plugin? I''m > trying to mock Session from the restful_authentication plugin but I > get a number of errors telling me RSpec doesn''t recognize Session. > > NameError in ''SessionsController handling GET /sessions/new should > be successful'' > uninitialized constant SessionYour problem is that the class isn''t just Session, it''s CGI::Session::ActiveRecordStore::Session Brandon
Dan, you can take a look at what *court3nay did, here: * ** http://sample.caboo.se/empty_rails_app/trunk/spec/controllers/session_controller_spec.rb Robert On 10/18/07, Brandon Keepers <bkeepers at gmail.com> wrote:> > Dan, > > On Oct 18, 2007, at 6:14 PM, Dan Croak wrote: > > > Hey folks, > > > > How do I mock a model that is given to me by a Rails plugin? I''m > > trying to mock Session from the restful_authentication plugin but I > > get a number of errors telling me RSpec doesn''t recognize Session. > > > > NameError in ''SessionsController handling GET /sessions/new should > > be successful'' > > uninitialized constant Session > > Your problem is that the class isn''t just Session, it''s > CGI::Session::ActiveRecordStore::Session > > Brandon > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- - Robert http://robertrevans.com http://rubysnips.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071019/1d29f70f/attachment.html