← Lua Academy

Testing the platform · 53 / 69

Keep invalid input local

YOUR TASK

Make this happen

3 steps
  1. Validate before calling the host. An empty key should return false without attempting a cache delete. Strict mocks help expose unintended calls.

  2. Read the cases in lesson.test.lua, then choose Check solution to see what needs to change.

  3. Update subject.lua until every test passes. Keep the supplied tests unchanged.

Read the starter and tests

subject.lua

exports.remove = function(key: string): boolean
  local ok = ctx.cache.delete(key)
  return ok
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("empty key performs no call", function()
  local s = subject.load()
  t.expect(s.remove("")):eq(false)
  t.expect(t.mock.spy("ctx.cache.delete")):never_called()
end)
t.it("valid key is forwarded", function()
  local s = subject.load()
  t.mock.host("ctx.cache.delete"):returns(true, nil)
  t.expect(s.remove("order")):eq(true)
end)