← Lua Academy

Testing the platform · 49 / 69

Verify the requested key

YOUR TASK

Make this happen

3 steps
  1. A spy records how a mocked host function was called. Forward the caller’s key unchanged instead of hard-coding a cache key.

  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.read = function(key: string)
  return ctx.cache.get("fixed")
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("forwards the key", function()
  local s = subject.load()
  t.mock.host("ctx.cache.get"):returns(true, "x")
  s.read("order:42")
  t.expect(t.mock.spy("ctx.cache.get")):called_with("order:42")
end)