Build Status Gem Version


Testing Trailblazer applications usually involves the following tests.

  1. Unit tests for operations: They test all edge cases in a nice, fast unit test environment without any HTTP involved.
  2. Integration tests for controllers: These Smoke tests only test the wiring between controller, operation and presentation layer. Usually, a coded click path simulates you manually clicking through your app and testing if it works. The preferred way here is using Rack-test and Capybara.
  3. Unit tests for cells By invoking your cells with arbitrary data you functionally test the rendered markup using Capybara.

All the up to date details on the available assertions and helpers is available at official documentation.


To use available assertions, add in your test _helper the following modules:

include Trailblazer::Test::Assertions
include Trailblazer::Test::Operation::Assertions

If you are using Trailblazer v2.0 you need to add also:

require "trailblazer/test/deprecation/operation/assertions"

include Trailblazer::Test::Deprecation::Operation::Assertions # in your test class

Learn more


Use assert_pass to run an operation and assert it was successful, while checking if the attributes of the operation's model are what you're expecting.

it { assert_pass Blog::Operation::Create, { params: { title: "Ruby Soho" } }, title: "Ruby Soho" }


To test an unsuccessful outcome of an operation, use assert_fail. This is used for testing all kinds of validations. By passing insufficient or wrong data to the operation, it will fail and mark errors on the errors object.

it { assert_fail Blog::Operation::Update, { params: { band: nil } }, expected_errors: [:band] }


This will test that the operation fails due to a policy failure.

it { assert_policy_fail Blog::Operation::Delete, ctx({title: "Ruby Soho"}, current_user: not_allowed_user) }


Test attributes of an arbitrary object.

it { assert_exposes model, title: "Timebomb", band: "Rancid" }


There are several helpers to deal with operation tests and operations used as factories. Add this in your _helper.rb file to use all available helpers.

include Trailblazer::Test::Operation::Helper

Learn more


Instead of manually invoking an operation, you can use the call helper.

it do
  result = call Blog::Operation::Create, params: {title: "Shipwreck", band: "Rancid"}
  # use `result` object however you want


The factory method calls the operation and raises an error should the operation have failed. If successful, it will do the exact same thing call does.

it do
  assert_raises do
    factory Blog::Operation::Create, params: {title: "Shipwreck", band: "The Chats"}


This helper allows you to mock any step within a given or deeply nested activities. For example,

class Show < Trailblazer::Operation
  step :load_user

To skip processing inside :load_user and use a mock instead, use mock_step.

it do
  new_activity = mock_step(Show, id: :load_user) do |ctx|
    ctx[:user] ='Mocky')

  assert_pass new_activity, {}, {} do |ctx|
    assert_equal ctx[:user].name, 'Mocky'


Add this line to your application's Gemfile:

gem 'trailblazer-test'

And then execute:

$ bundle

Or install it yourself as:

$ gem install trailblazer-test