Lua

Lesson 10

Busted test cases

Write Lua tests around observable module behavior instead of only checking that functions return truthy values.

Good Code

spec/review_rules_spec.lua
describe("review rules", function()
  it("rejects a missing title", function()
    -- The fixture names the invalid input the module must reject.
    local ok, error = ReviewRules.validate({ title = nil, score = 5 })

    assert.is_false(ok)
    assert.are.equal("title_required", error.code)
  end)
end)

Bad Code

spec/review_rules_spec.lua
describe("review rules", function()
  it("validates reviews", function()
    -- Truthy checks miss the actual validation contract.
    assert.is_truthy(ReviewRules.validate({}))
  end)
end)

Review Notes

What to review

Good Code

The good version names the invalid input and asserts the exact error code. A regression in title validation will fail clearly.

Bad Code

The bad version only checks for a truthy result. It does not protect the validation contract that callers rely on.

Takeaways

  • Lua tests should name the behavior, build a small fixture, and assert the exact output or error shape.