← Lua Academy

Testing the platform · 47 / 69

Read a mocked cache

YOUR TASK

Make this happen

3 steps
  1. The exported contract describes ctx.cache.get as an (ok, value) call. A strict mock supplies both results. No real cache or network request is involved.

  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.cached = function(key: string)
  return nil
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("reads supplied value", function()
  local s = subject.load()
  t.mock.host("ctx.cache.get"):returns(true, "ready")
  t.expect(s.cached("job")):eq("ready")
end)