[rspec-users] Loading (or stubbing) data before model loading

David Chelimsky dchelimsky at gmail.com
Wed Jul 22 08:57:59 EDT 2009

On Tue, Jul 21, 2009 at 2:58 PM, Tom Stuart<tom at experthuman.com> wrote:
> Hi,
> A Rails project I'm working on has, for better or worse, some data-driven
> structure in its models: a few model classes need certain values (e.g. some
> enumerations) to be available in the database at load time in order for
> certain bits of metaprogramming to kick off correctly. It would be possible
> to do this all lazily instead of when the classes load, but in reality the
> database values are always there and everything already works beautifully so
> it's hard to justify putting in the effort required to be that lazy.
> The problem is that RSpec starts with an empty test database

Not exactly. RSpec starts with whatever database you have and rolls
back to that state after each example provided you're only using
before(:each) to set up state before each example.

Any pre-existing database state will remain, and any state that you
set up in before(:suite) or before(:all) will not be rolled back
unless you do so explicitly.


> and I can't see
> the best way or place to prepopulate it before the models get loaded (i.e.
> when Rails bootstraps, since config.cache_classes = true), ideally so that
> both rake spec and script/spec do the correct setup in time. It would be
> incredibly convenient to be able to use Fixtures.create_fixtures to do the
> loading so that the required data can live in YAML fixtures, but maybe it's
> optimistic to try to do that before Rails has loaded.
> Or maybe I'm just going about this completely the wrong way, and someone has
> a clever idea about what the right way is? That would be brilliant.
> Cheers,
> -Tom
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list