Closed ffbh123456 closed 3 years ago
Hello! Yes, you can override the behavior of your function using stubs.
You want a return value, so you need to use Spies. Here's a simple code example:
-- function you want to test
function B()
local result = A("my argument")
return result
end
describe("Some", function ()
_G.A = function ()
-- set your result
return 42
end
it("mock", function ()
spy.on(_G, "A")
assert.are.equals(B(), 42) -- check function B result
assert.spy(A).was_called_with("my argument") -- check with what arguments function A was called
A:revert()
end)
end)```
@NickFlexer Hi, Thank you, it works. But I encountered a more complicated situation, please see the picture below, how do I customize the value of result1 and result2 in busted unit test
if you want a mock package that is called by require
you must follow these steps:
package.preload
functionrequire
example:
describe("Test module", function ()
it("load module", function ()
-- 1
local m = {
start = function ()
return 42
end
}
-- 2
local database = mock(m)
-- 3
package.preload["database"] = function ()
return database
end
-- 4
local test_module = require "test_module"
-- 5
assert.are.equals(test_module.B(), 42)
assert.stub(database.start).was.called_with("my argument")
-- 6
mock.revert(database)
end)
end)
@NickFlexer Thank you!!! you are really helpfully and explained in detail
I have two functions:
Function A needs to connect to the database. I don’t want to connect to the database in my unit test, but this function has a return value. The return value is used in function B. I hope that I can get my own customization function result without actually executing function A. Is there any way to return the customization function value?