Answer the question
In order to leave comments, you need to log in
How to use stub/mock in lua (busted)?
function processEvent(e)
eventId = getEventType(e)
if eventId == 3 then
return 3
else
return 0
end
end
package.path = "../?.lua;" .. package.path
describe("processEvent", function()
it("EventId==3", function()
function getEventType() return 3 end
chunk = assert(loadfile("templates/sr.lua"))
chunk()
assert.are.equal(processEvent('hello'), 3)
end)
end)
Answer the question
In order to leave comments, you need to log in
I tried to test something like this using stub:
it("calls getEventType with the event", function()
function getEventType()
return 3
end
stub(_G, "getEventType")
processEvent(42)
assert.stub(getEventType).was.called_with(42)
getEventType:revert()
end)
It is not entirely clear how to correctly load code from another file if the code is not wrapped in a module.Use the dofile function. But it's better to make modules from the tested files and load them using require.
-- processEvent.lua
function getEventType(e)
...
end
function processEvent(e)
eventId = getEventType(e)
if eventId == 3 then
return 3
else
return 0
end
end
-- process_event_spec.lua
require 'processEvent'
describe("processEvent", function()
it("calls getEventType with the event", function()
stub(_G, "getEventType")
processEvent(42)
assert.stub(getEventType).was.called_with(42)
getEventType:revert()
end)
end)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question