-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
25 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,45 @@ | ||
# Trailblazer::Test | ||
|
||
Testing a Trailblazer project is very simple. Your test suite usually consists of two separate layers. | ||
_Assertions and helpers for operation unit tests._ | ||
|
||
* Integration tests or system tests covering the full stack, and using Capybara to “click through” the happy path and possible edge-cases such as an erroring form. Smoke tests make sure of the integrity of your application, and assert that controllers, views and operations play well together. We will provide more documentation about system tests shortly. | ||
* Operation unit tests guarantee that your operations, data processing and validations do what they’re supposed to. As they’re much faster and easier to write than full stack “smoke tests” they can cover any possible input to your operation and help quickly asserting the created side-effects. The trailblazer-test gem is here to help with that. | ||
The [comprehensive docs are here](https://trailblazer.to/2.1/docs/test/). | ||
|
||
There’s no need to test controllers, models, service objects, etc. in isolation - unless you want to do so for a better documentation of your internal APIs. As operations are the single entry-point for your functions, your entire stack is covered with the two test types. | ||
Read our introducing blog post for a better overview. | ||
|
||
The trailblazer-test gem allows simple, streamlined operation unit tests. If you fancy RSpec, [rspec-trailblazer-test](https://github.com/trailblazer/rspec-trailblazer-test/) is here for you. | ||
## Installation | ||
|
||
## Documentation | ||
Add the following line to your project's `Gemfile`. | ||
|
||
The TRB website has [extensive documentation on this gem](https://trailblazer.to/2.1/docs/test.html). | ||
```ruby | ||
gem "trailblazer-test", ">= 1.0.0", "< 2.0.0" | ||
``` | ||
|
||
## Overview | ||
|
||
For operation unit tests, this gem provides `assert_pass` and `assert_fail`. | ||
This gem adds the following assertions and helpers: | ||
|
||
```ruby | ||
# test/operation/song_operation_test.rb | ||
class SongOperationTest < OperationSpec | ||
|
||
# The default ctx passed into the tested operation. | ||
let(:default_ctx) do | ||
{ | ||
params: { | ||
song: { # Note the {song} key here! | ||
band: "Rancid", | ||
title: "Timebomb", | ||
# duration not present | ||
} | ||
} | ||
} | ||
end | ||
* `#assert_pass` to test an operation terminating with success. | ||
* `#assert_fail` to assert validation errors and the like. | ||
* `#mock_step` helping the replace steps with stubs. | ||
|
||
# What will the model look like after running the operation? | ||
let(:expected_attributes) do | ||
{ | ||
band: "Rancid", | ||
title: "Timebomb", | ||
} | ||
end | ||
## Example | ||
|
||
let(:operation) { Song::Operation::Create } | ||
let(:key_in_params) { :song } | ||
An example test case checking if an operation passed and created a model could look as follows. | ||
|
||
it "passes with valid input, {duration} is optional" do | ||
assert_pass( {}, {} ) | ||
end | ||
```ruby | ||
# test/operation/memo_test.rb | ||
|
||
it "converts {duration} to seconds" do | ||
assert_pass( {duration: "2.24"}, {duration: 144} ) | ||
end | ||
require "test_helper" | ||
|
||
it "converts {duration} to seconds" do | ||
assert_pass( {duration: "2.24"}, {duration: 144} ) do |result| | ||
assert_equal true, result[:model].persisted? | ||
end | ||
end | ||
class MemoOperationTest < Minitest::Spec | ||
Trailblazer::Test.module!(self) # install our helpers. | ||
|
||
it "fails with missing {title} and invalid {duration}" do | ||
assert_fail( {duration: 1222, title: ""}, [:title, :duration] ) | ||
it "passes with valid input" do | ||
# ... | ||
assert_pass Memo::Operation::Create, input, | ||
content: "Stock up beer", | ||
persisted?: true, | ||
id: ->(asserted:, **) { asserted.id > 0 } | ||
end | ||
# ... | ||
end | ||
``` |