From 0990b0fde2b7b895dd5d9d49478b0e2506ae310f Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sun, 31 Mar 2019 12:47:34 -0600 Subject: [PATCH] Add before/after hook example --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 2620bde..59846b1 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,29 @@ Contexts can have values defined for multiple tests (`let` and `subject`). Additionally, hooks can be used to ensure any initialization or cleanup is done (`before`, `after`, and `around`). Pre- and post-conditions can be used to ensure code contracts are kept. +```crystal +# Initialize the database before running the tests in this context. +before_all { Database.init } + +# Teardown the database and cleanup after tests in the is context finish. +after_all { Database.cleanup } + +# Before each test, add some rows to the database. +let(row_count) { 5 } +before_each do + row_count.times { Database.insert_row } +end + +# Remove the rows after the test to get a clean slate. +after_each { Database.clear } + +describe "#row_count" do + it "returns the number of rows" do + expect(Database.row_count).to eq(row_count) + end +end +``` + Spectator has different types of contexts to reduce boilerplate. One is the `sample` context. This context type repeats all tests (and contexts within) for a set of values.