← Lua Academy

Using the ctx · 61 / 69

Assert structured logging

YOUR TASK

Make this happen

3 steps
  1. Remove the payload attribute while retaining the job identifier and one log call. Use the same mocking pattern for ctx.log.warn or ctx.log.error when testing other branches.

  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.record = function(jobId: string, payload: string)
  ctx.log.info("Job accepted", { jobId = jobId, payload = payload })
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")

t.it("logs a useful identifier without the payload", function()
  t.mock.host("ctx.log.info"):returns(nil)
  local s = subject.load()
  s.record("job:42", "private customer data")
  t.expect(t.mock.spy("ctx.log.info")):called_once()
  t.expect(t.mock.spy("ctx.log.info"))
    :called_with("Job accepted", { jobId = "job:42" })
end)