We work with Ruby on Rails
Since working with Ruby on Rails we’ve seen the tools we use shift faster than we can keep our website updated. However the intent of the tools has stayed the same. Here are some of our best practices and favorite plugins/gems at the time of this writing.
Basic Patterns
We try to keep our classes small and have single responsibility. This includes keeping SQL out of the controller, logic out of the views and html out of the models. We use tools such as Injection Plugin and patterns such as Jay Fields’s Presenter to help aid such development.
Injection Plugin
Injection is dependency injection for Rails, allowing you to have objects injected into controllers which can be easily replaced by mocks in controller tests. Use Injection to hide details of complex logic which needs to be invoked in a controller. Some examples include:- parsing, validating and saving an uploaded file
- interacting with a payment gateway service
- building a complicated report data structure which needs to be rendered
Unit Tests
Currently RSpec seems to be driving most of our projects’ unit test
suites. From model testing to controllers RSpec has quickly become the
defacto unit testing framework for ruby on rails.
Migration Test Helper plugin
Migrations can cause many headaches. From failing migrations that leave your database in an unusable state to old migrations that can no longer be run. Migration test helper lets you:- Test drive database changes
- Test migrating from 0 to the latest, and back again.
- Verify that data transformation migrations work as expected
- Verify that your old migrations are still runnable
Mocking
We all agree that mocking has it’s place in our testing suite but what mocking library to use is something we’ll most likely never agree on. Currently we’ve got projects that use HardMock, Mocha and RSpec mocking library.
Integration Testing
With all the magic rails does behind the scenes we’ve found system-level tests to be an extremely important step in our rails development process.
Over the years, we’ve used a number of different tools for our primary suite of system tests, including Rails built-in integration test framework, Selenium for real in-browser testing, and Celerity for simulated browser testing. To write these tests we use a number of different testing systems, including the default Test::Unit and Cucumber.
Test Data
On many projects we use a combination of strategies for managing test data. We often use fixtures for things that are needed for every test, such as settings, user-editable templates, etc. For domain objects, we often generate those on a test by test basis using an object mother such as Factory Girl. We’ve found the randexp is very useful for generating names, email addresses, urls, etc. when populating fields on our generated models.
Markup
A lot of us like Haml for its concision, readability, and flexibility.