Skip to content

Writing tests

Amethyst.Test is an RSpec-inspired testing framework. It is an ordinary package rather than part of the SDK, so it can be swapped for another framework:

Terminal window
am add Amethyst.Test
am test
describe UserService do
let(:service) { UserService.new }
before do
# runs before each example
end
context "when the user is valid" do
it "saves the user" do
user = User.new(name: "Alice")
expect(service.save(user)).to be_truthy
end
end
context "when the user is invalid" do
it "raises an error" do
expect { service.save(nil) }.to raise_error(ValidationException)
end
end
end

Group related examples. describe usually names the class or method under test; context describes a situation. Both nest, and both accept a block.

Defines one example. The string describes the expected behaviour and appears in test output.

Define a named value for the examples in a group. let is lazy: the block runs the first time an example uses the name. let! is eager: the block runs before every example. Each example gets a fresh value.

Hooks that run before and after each example in the group, including examples in nested groups.

expect(actual).to matcher
expect { code }.to matcher

States an expectation. Pass a value for most matchers, or a block for matchers that need to run code, such as raise_error.