Skip to content

Latest commit

 

History

History
83 lines (63 loc) · 2.95 KB

File metadata and controls

83 lines (63 loc) · 2.95 KB

Overview of Approval Tests

Contents

Summary

Approval Tests are an alternative way to write asserts in your test code.

As the following examples demonstrate, they give significantly simpler tests of complex objects.

Traditional Asserts

Traditional tests spend equal time focusing on creating the inputs and verifying the outputs.

When the objects being tested are non-trivial, either the tests become quite verbose (as shown in this example), or it's tempting to only test a small part of the behaviour.

// Arrange, Act
Sandwich s = createSandwichForTest();
// Assert
REQUIRE("Sourdough" == s.getBread());
REQUIRE(s.getCondiments().contains("Mayo"));
REQUIRE(s.getCondiments().contains("Pepper"));
REQUIRE(s.getCondiments().contains("Olive Oil"));
REQUIRE(s.getFillings().contains("Tomato"));
REQUIRE(s.getFillings().contains("Lettuce"));
REQUIRE(s.getFillings().contains("Cheddar"));

snippet source / anchor

Approval Tests

Approval Tests simplify verification of the outputs. They do this by writing the outputs to a file, which once saved, becomes your spec.

You still supply the inputs, but Approval Tests gives you powerful ways of viewing complex outputs, meaning you can move on to the next feature or next test more quickly.

// Arrange, Act
Sandwich s = createSandwichForTest();
// Assert
Approvals::verify(s);

snippet source / anchor

This generates the approval file - which is generated for you, but approved by you.

sandwich {
    bread: "Sourdough",
    condiments: ["Mayo", "Pepper", "Olive Oil"],
    fillings: ["Tomato", "Lettuce", "Cheddar"]
}

snippet source / anchor


Back to User Guide