[rspec-users] Keeping unit tests from hitting the DB

David Chelimsky dchelimsky at gmail.com
Thu Aug 23 17:43:44 EDT 2007

On 8/23/07, Ben Mabey <ben at benmabey.com> wrote:
> Hey all,
> I think I saw a thread about this earlier (maybe it was on the devlopers
> list) but I am wondering if anyone has stubbed out AR so that the unit
> test do not hit the database.  The reason I am curious is because there
> was a lightning talk given at the Ruby Hoedown (watch it here:
> http://rubyhoedown2007.confreaks.com/session06.html) about separating
> unit tests from the DB entirely and making it a lot faster.  I'm not
> just talking about not using fixtures but this would not even query the
> DB for columns or do any inserts or updates either.  Dan Manges was the
> presenter and he talks about it on his blog and his newly released gem:
> http://www.dcmanges.com/blog/rails-unit-record-test-without-the-database
> I haven't experimented with the gem yet but I'm sure it could be changed
> to work with rspec.  I'm curious about people's opinions about this
> practice and see if anyone has done something similar with rspec
> already.

I've heard of ppl experimenting w/ this but haven't seen it in action.
I would LOVE to see this capability but I'd like to be able to control
it from behaviour to behaviour. For example, I tend to mock all models
in view, controller and helper specs, but not in model specs, where I
tend to let them interact w/ the DB. I'm not recommending that
approach over mocking models in model specs, just saying that it's my
personal preference. There are very good arguments to do it

That said, I'd want to be able to do this in spec_helper.rb:

Spec::Runner.configure do |config|
  config.deny_db_access :view, :controller, :helper

or _something_ like that. Then if a controller spec tries to hit the
db you should get an error.

Feel like contributing that?

> I think the speed is very attractive but if you were to do
> this you would need to to have another type of test that would actually
> hit the DB.  Has anyone done this with rspec that could shed a little
> more light on the advantages/disadvantages?
> Thanks,
> Ben
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list