One-liner tests for common Rails functionality

The shoulda-matchers gem is a cool way to write tests for Rails apps. I’m using it for my open-source OKR tracker, OpenOKR (I’m looking for contributors!), which features a Rails API backend. The library abstracts away a lot of the code needed to write effective tests for common functionality in Rails applications. Here’s an example from OpenOKR that should be self-explanatory:

require 'rails_helper'

RSpec.describe KeyResult, type: :model do
  it { should belong_to(:objective) }
  it { should belong_to(:user) }

  it { should { should validate_presence_of(:description) } }
  it { should { should validate_presence_of(:due_date) } }
end

You can check out the whole test suite I’m building here.

Show Comments

Get the latest posts delivered right to your inbox.