Testing Functions
Test DSL with describe/it blocks, assertions, and factory functions.
Test DSL
describe(description, def)
Define a test group. context is an alias.
test(description, def)
Define an individual test case.
it(description, def)
Alternative to test(). specify is an alias.
Example
describe("Calculator", def() {
test("adds numbers", def() {
assert_eq(add(1, 2), 3)
})
it("subtracts numbers", def() {
assert_eq(subtract(5, 3), 2)
})
describe("division", def() {
it("divides numbers", def() {
assert_eq(divide(10, 2), 5)
})
it("handles division by zero", def() {
# Test error handling
})
})
})
Setup & Teardown
before_each(def)
Run before each test
after_each(def)
Run after each test
before_all(def)
Run once before all tests
after_all(def)
Run once after all tests
Assertions
assert_equal(expected, actual, message)
Asserts that two values are equal.
assert_equal(42, result, "should return 42");
assert_equal("hello", str, "string should match");
assert_equal(true, is_valid, "should be valid");
assert_true(value, message)
Asserts that a value is true.
assert_true(user.is_active, "user should be active");
assert_true(contains(items, "test"), "should contain test item");
assert_false(value, message)
Asserts that a value is false.
assert_false(user.is_blocked, "user should not be blocked");
assert_false(is_empty(items), "items should not be empty");
assert_contains(haystack, needle, message)
Asserts that a collection contains a specific value.
assert_contains(users, "admin", "should contain admin user");
assert_contains([1, 2, 3], 2, "should contain 2");
assert_nil(value, message)
Asserts that a value is nil (null).
assert_nil(result.error, "should have no error");
assert_nil(user.deleted_at, "should not be deleted");
assert_not_nil(value, message)
Asserts that a value is not nil.
assert_not_nil(user.id, "user should have an id");
assert_not_nil(response.body, "response should have body");
Assertion Result
All assertion functions return a result hash with the following structure:
{
"passed": true,
"message": "description of the test",
"expected": value_that_was_expected,
"actual": value_that_was_actual
}
assert_not(condition)
Assert condition is false
assert_eq(a, b)
Assert a equals b
assert_ne(a, b)
Assert a does not equal b