Forum: RSpec Including spec/rails/mocks into other classes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
27565572097bf4c73e2a5617b29682db?d=identicon&s=25 Cameron Booth (Guest)
on 2008-10-29 18:46
(Received via mailing list)
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
C694a032be7518a0d704318895f8fe1d?d=identicon&s=25 Ben Mabey (mabes)
on 2008-10-30 04:37
(Received via mailing list)
Cameron Booth 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
> rspec-users@rubyforge.org
> 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
944f769c99deff7aa8bc3b5b93830b7a?d=identicon&s=25 Scott Taylor (Guest)
on 2008-10-30 05:56
(Received via mailing list)
On Oct 29, 2008, at 11:27 AM, Cameron Booth 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
This topic is locked and can not be replied to.