DataMapper is awesome. It’s flexible, you define the properties in the model itself, it deals with all sorts of columns, it can create UUIDs easily, it is fantastic for dealing with legacy, or just not designed with Rails’ ActiveRecord in mind databases. Hell, it shoveled my driveway, hooked up my cable, and delivered our beautiful, 7 pound, 2 ounce baby boy last Tuesday. (I kid! I don’t have a baby. Yet.)
It can also connect to multiple databases, even when used as the ORM in
a Rails 3 app. Unfortunately, this isn’t mentioned on DataMapper’s
website, that I could find. However, people
with their wits about them (I am not in that set of people) might think
to check out the README for the dm-rails project on
Github. In there, it shows
how to lay out your
database.yml properly. It’s important. Go see it.
Go. I’ll wait.
I mention this, and forced you to go look, mostly to help someone searching frantically. You there. Guy who has been searching for this frantically in futile frustration for an hour: Your journey is over. Let’s move on.
I had to do a bit of work to get RSpec and Cucumber behaving correctly.
I want my test databases, or repositories, cleared after each test. I
solved RSpec first, so we’ll start with that. My
looks something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Clearly the code above is specific to MySQL. If you’d rather avoid shackling a specific database server to your app’s ankle, you might try something like database_cleaner.
The interesting part is probably
DataMapper.repository(model.default_repository_name). I define
self.default_repository_name differently for models that aren’t using,
:default. That allows this to work (among other things in
DataMapper). I recommend it. For instance:
1 2 3 4 5 6 7
I needed Cucumber to do some of this crazy junk also, of course. The
dm-rails gem recommended a before and after block to add to a
features/support/datamapper.rb, but I had to add a bit to mine to make
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Familiar, right? That should get your testing mojo rising. It’s basically opening the door for DataMapper, so he can come in and repaint your nursery while teaching your newborn sign language, or one of his many other talents.