← Lua Academy

Testing the platform · 51 / 69

Use an argument-aware fake

YOUR TASK

Make this happen

3 steps
  1. A function fake computes responses from its arguments. The production function should use the requested key and handle the success flag.

  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)
  local ok, value = ctx.cache.get("a")
  return value
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("fake depends on key", function()
  local s = subject.load()
  t.mock.host("ctx.cache.get"):using(function(key)
    return true, "value:" .. key
  end)
  t.expect(s.read("b")):eq("value:b")
end)