← Lua Academy

Testing the platform · 48 / 69

Handle a failed host call

YOUR TASK

Make this happen

3 steps
  1. A failed async-style host call returns false and an error message. Do not return that message as successful business data.

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

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("failure gives no data", function()
  local s = subject.load()
  t.mock.host("ctx.cache.get"):returns(false, "unavailable")
  t.expect(s.cached("job")):is_nil()
end)
t.it("success preserves data", function()
  local s = subject.load()
  t.mock.host("ctx.cache.get"):returns(true, "ready")
  t.expect(s.cached("job")):eq("ready")
end)