How to mock kong variable using Busted

Hi Team,
I am trying to write test cases for my Lua script. I am using Busted framework to write the test cases.

In my test script, I am trying to pass the Kong internal variable “conf” from the test script, however, I am getting nil while I am executing the test case.

In my handler.lua, I have function access which is taking conf as an argument.
Sample code:
function M:access(conf)
M.super.access(self)
local ok, err = do_authentication(conf)
if not ok then
return kong.response.exit(err.status, { message = err.message }, err.headers)
end
end
Sample test script :

require “busted”
expose(“an exposed test”, function()
package.path="/home/POC_lua/luaossl-master/examples/handler.lua"
local handler = require(‘handler’)

describe(“Test void lua script”, function()
it(‘Mutual Handler Test Cases’, function()
local session_params = “xyz”

local sess_con = handler.access(session_params )

and I am running the test script by -> busted busted_test.lua command and I am getting conf as nil.

Please let me know how to mock conf variable here.

Thanks.

Take a look at https://docs.konghq.com/1.1.x/plugin-development/tests/. If you are looking to have conf populated then I would say you are ultimately looking to write an integration test rather than a unit test. There is a helpers module spec.helpers available within the Kong source that will allow you to do just that. Essentially you can use this module to configure a running instance of Kong and then issue requests against that in each of your tests. This way, conf, will be correctly populated by conf when your custom plugin code is called. You can then use the result returned to assert whatever it is you want to test. This is all described in the link above and numerous examples can be found in the Kong test suite at https://github.com/Kong/kong/tree/master/spec.

@onematchfox
I am trying to write the unit test here.
I am passing the conf value from the test script however the value is overridden by kong conf value and I am getting nil.
So I need to mock the kong global variables and nginx variables. Could please share any document to mock the nginx variables using busted?

The example in the link I sent you does exactly that.

Currently you seem to be trying to call the access method on your plugin directly - local sess_con = handler.access(session_params ). Don’t do that. Rather make use of the helpers module to make a request via kong. I.e. local res = proxy_client:get("/get", .... from the link. This will ensure that conf is correctly populated when your plugin code is called.