#377 open
Ben Mabey

Add ability to restrict database access on certail rails example groups

Reported by Ben Mabey | April 13th, 2008 @ 08:16 AM | in No-Milestone-Assigned

Back in August we talked about adding the ability to turn off DB access for certain types of example groups in rspec_on_rails:

http://rubyforge.org/pipermail/r...

The idea was that whenever someone tried to hit the DB under a group that had it turned off an exception would be raised.

I decided to revisit the problem and attempt a patch because the functionality would be very useful at my company so developers are yelled at with they attempt to use real models in a layer we have decided should be primarily interaction-based.

Anyways, I have finally put together the beginning of a patch, with documentation and all:

http://github.com/bmabey/rspec-r...

It is not quite there yet though. If you look at my comments you'll see I'm not too happy with the way I am caching my columns. I cache it by allowing a connection to go through once to get the column names. UnitRecord solves this problem by inspecting the schema.rb file. So we could do that but I thought I should throw it for discussion before I wasted any more time. :)

I also said that mock_model was problematic. I somewhat take that back.. The only places where it causes a problem is when the model being mocked has some DB calls in the class definition, like so:

class Thing < AR::Base

@@names = find(:all).map(&:name)

....

end

(Because by using the model's constant rails will load it into memory.)

This surprised me at first and I thought by patch was faulty but after thinking about it I think that this should be the desired behaviour. This will let the developer know that their seemingly harmless view spec is actually hitting the DB when mock_model is called on such a model which could slow them down a lot. The problem can easily be resolved in most cases by refactoring the call into a lazy one that is then cached. This is probably best practice anyways.

Also, if that was too much of a problem for certain models db access could be turned on explicitly for that example group.

Anyways, I'm looking for feedback on the patch. And if you want to accept it please let me know any changes you would like me to make.

Thanks,

Ben

p.s. Sorry for such the long post...

Comments and changes to this ticket

  • David Chelimsky

    David Chelimsky April 13th, 2008 @ 08:20 PM

    • → Assigned user changed from “” to “David Chelimsky”
    • → State changed from “new” to “open”
  • David Chelimsky

    David Chelimsky April 14th, 2008 @ 04:29 AM

    Hey Ben - in terms in spirit, layout, etc, this seems sound to me. I don't want to get nit-picky until you've got all the functionality you want working as you want. Good?

  • Ben Mabey

    Ben Mabey May 16th, 2008 @ 11:09 PM

    After I wrote this patch I found out about this project:

    http://avdi.org/projects/nulldb/

    I like the design of it better than what I have done in my patch. So maybe the patch isn't really needed and people can just use this plugin if they really want this functionality.

    Maybe we should just mark this ticket as invalid? WDTY?

    -Ben

  • David Chelimsky

    David Chelimsky May 17th, 2008 @ 03:35 AM

    There is also a method in git named stub_model that returns a real

    model object that will complain if you try to hit the db.

    On May 16, 2008, at 5:10 PM, Lighthouse

    wrote:

  • Ben Mabey

    Ben Mabey May 17th, 2008 @ 07:05 AM

    True, and I think that is perfect for view specs. Of course that will only yell at you when you use it. If someone on your team doesn't know better they might just use a regular model. That was my original motivation, but it seems like this new addition to rspec and the nulldb plugin will do the job quite nicely.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Behaviour Driven Development for Ruby.

Shared Ticket Bins

People watching this ticket