← Lua Academy

Testing the platform · 52 / 69

Mock a side effect

YOUR TASK

Make this happen

3 steps
  1. Host writes are mocked too. ctx.cache.set accepts a record with key, value and optional ttl. Preserve the requested key and value.

  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.save = function(key: string, value: string)
  return ctx.cache.set({ key = "fixed", value = value })
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("writes requested record", function()
  local s = subject.load()
  t.mock.host("ctx.cache.set"):returns(true, nil)
  s.save("order", "ready")
  t.expect(t.mock.spy("ctx.cache.set"))
    :called_with({ key = "order", value = "ready" })
end)