← Lua Academy

Testing the platform · 50 / 69

Avoid duplicate calls

YOUR TASK

Make this happen

3 steps
  1. Repeated host calls can duplicate work or side effects. The called_once matcher asserts that a single request causes one cache read.

  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)
  ctx.cache.get(key)
  return ctx.cache.get(key)
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("one request", function()
  local s = subject.load()
  t.mock.host("ctx.cache.get"):returns(true, "x")
  s.read("order")
  t.expect(t.mock.spy("ctx.cache.get")):called_once()
end)