testing your way to success

I have been immersed in production codebases for my Fohr Card and Fractured Atlas projects. At first, it feel initimidating trying to figure out all of the models and related associations. I have found that a great way to wrap your head around the code base is to create a new branch and test the code.

A good basic set-up is to start unit testing the models. In order to do this depends on if your codebase is using a relational database or something like Mongoid.

Relational Databases

{% codeblock Gemfile lang:ruby %}

group :development do
  gem 'guard-rspec', require: false
end

group :test do
   gem 'rspec-rails'
   gem 'factory_girl_rails'
   gem 'faker'
   gem 'shoulda-matchers'
   gem 'terminal-notifier-guard' 
end

{% endcodeblock %}

  • Guard RSpec waits for you to save your files and automatically runs your tests, thereby reducing the time needed to run tests.
  • I like the RSpec syntax over Unit Test and Mini Test.
  • Factory Girl Rails creates objects for your tests. This gem deserves its own blog post.
  • The Faker Gem is used in conjunction with Factory Girl Rails to create fake data.
  • I like to use Shoulda Matchers to test validaitons and associations in the code. This gem and firing up Rails Console to play with the objects in my testing is the crux to learning a new codebase.
  • Some people find Terminal Notifier annoying, but it helps me know if my tests pass when I have Sublime maximized on my 11 inch MacBook Air screen.

Mongoid

{% codeblock Gemfile lang:ruby %}

group :development do
  gem 'guard-rspec', require: false
end

group :test do
  gem 'fabrication'
  gem 'faker'
  gem 'rspec-rails'
  gem 'mongoid-rspec'
  gem 'terminal-notifier-guard' 
end

{% endcodeblock %}

The gems for Mongoid testing are similar to those used in testing with a relational database.

  • The Fabrication gem is similar to building objects with Factory Girl, except the documentation website rocks.
  • The Mongoid RSpec gem is similar to Shoulda-Matchers

Gems

Guard RSpec
RSpec Rails
Factory Girl Rails
Faker
Shoulda Matchers
Terminal Notifier Guard
Fabrication
Mongoid RSpec

Other Resources

I found this book invaluable in learning best practices in testing: Everday RSpec
h/t Victoria Friedman

comments powered by Disqus