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:
am add Amethyst.Testam testStructure
Section titled “Structure”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 endenddescribe / context
Section titled “describe / context”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.
let / let!
Section titled “let / let!”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.
before / after
Section titled “before / after”Hooks that run before and after each example in the group, including examples in nested groups.
expect
Section titled “expect”expect(actual).to matcherexpect { code }.to matcherStates an expectation. Pass a value for most matchers, or a block for
matchers that need to run code, such as raise_error.