[rspec-users] Loading (or stubbing) data before model loading
matt at mattwynne.net
Wed Jul 22 08:13:48 EDT 2009
On 21 Jul 2009, at 20:58, Tom Stuart wrote:
> 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 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
One challenge for you before we dig into how to solve it the way
you've suggested: do you really need to keep these enumerables in the
database, or could you keep them in code? That is, is there a valid
use case where they will be altered at run-time, or do they change so
seldom that it would be OK to have to release a new version of the
code when they need to be changed?
I ask because these things tend to get a lot easier if you're not
having to mess around with databases, so it's always worth pushing
back on that one first.
More information about the rspec-users