ObjectMother, or something like it, for Rails
XP Day Toronto 2007 is coming up soon, so I’m busy working on xpday.info, preparing it for the stampede of registrations we expect in the coming weeks. (I can dream.) In the process, I am writing Rails unit tests, which means using fixtures again. I wasn’t thrilled with my use of fixtures last time I coded Rails seriously, because it felt like the “One Dataset For All Tests” anti-pattern that I discovered so painfully on my first XP project. I would be much, much happier if I could have fixtures with the flexibility of named datasets per test, just as I can get with DbUnit. Still, I remembered
reading something about DRY database configurations and it gave me an idea for implementing a kind of ObjectMother pattern in YAML. This is what I did.
dummy: &dummy id: 10 where: "Nowhere, man" web_site_url: http://default.xpday.info start_date: 2007-01-01 registration_open_on: 2006-11-01 open: <<: *dummy id: 11 registration_open_on: 2006-12-01
The dummy fixture object must provide values for all the required properties, but should not provide values for the optional ones. The idea is for the properties to be overridable so that later fixture objects can specify only those property they care about specifying, accepting defaults for the rest. It seems pretty similar to ObjectMother to me! We’ll see how it works for me.