I’m reading a wonderful book named “Agile Web Development With Rails“, which is about how to develop web applications quickly (agilely) with Ruby on Rails.
In the chapter on how to do testing with rails, it gives out an interesting suggestion: use natural names like “Christmas_order”,”Fred” instead stiff names like “order1″,”customer1” we are used to. I really like this idea!
Just like the names of variables in general, you want to keep the names of fixtures as self-explanatory as possible. This increases the readability of the tests when you’re asserting that product(:valid_order_for_fred) is indeed Fred’s valid order. It also makes it a lot easier to remember which fixture you’re supposed to test against without having to look up p1 or order4. The more fixtures you get, the more important it is to pick good fixture names. So, starting early keeps you happy later.
But what to do with fixtures that can’t easily get a self-explanatory name like valid_order_for_fred? Pick natural names that you have an easier time associating to a role. For example, instead of using order1, use christmas_order. Instead of customer1, use fred. Once you get into the habit of natural names, you’ll soon be weaving a nice little story about how fred is paying for his christmas_order with his invalid_credit_card first, then paying his valid_credit_card, and finally choosing to ship it all off to aunt_mary.
Association-based stories are key to remembering large worlds of fixtures with ease.