Archive for June 2014
Controlling database usage in RSpec and factory_girl, part 2: association strategy and unstubbed queries
An important part of testing a Rails application is establishing the correct relationship between the specs of each layer and the database. To correctly test the layer that they test, some types of specs (model and feature specs) should run against the database; other types should not. As well as ensuring correctness, policing database usage optimizes performance: tests that use the database are slower.
This post, part 2 in a two-part series, describes two measures that you can take to minimize incorrect and unnecessary database usage in RSpec specs that use factory_girl.
Controlling database usage in RSpec and factory_girl, part 1: Choosing and allowing appropriate strategies
When testing each layer of a Rails application, it’s important to create test model instances in the right way for that layer. Some specs need model instances that have been saved in the database, or that appear to have been; others need instances that have not. It’s also important to not use the database when you don’t have to, because every use makes your tests take a little bit longer.
This post, part 1 in a two-part series, lays out the appropriate use of factory_girl in specs of each layer of Rails, and gives a way to enforce that usage.
Dark corners of unit-testing lore
The software community’s understanding of developer testing (unit testing, acceptance testing, etc.) has come a long way since “Extreme Programming Explained” and “Refactoring” and JUnit got everyone interested almost fifteen years ago. There was a time when developers argued whether unit testing was valuable (never mind TDD); now it’s taught in code camps as an essential skill.
But some testing techniques have remained out of the mainstream, for whatever reason. A couple of them came up on Stack Overflow recently and I was inspired to give long answers. Whether or not these techniques eventually become part of standard practice, they’re interesting to know about and possibly useful in shaping your thinking about mainstream practices.