Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking global file that is not avaliable #676

Open
RicardoMonteiroSimoes opened this issue Mar 17, 2022 · 0 comments
Open

Mocking global file that is not avaliable #676

RicardoMonteiroSimoes opened this issue Mar 17, 2022 · 0 comments

Comments

@RicardoMonteiroSimoes
Copy link

RicardoMonteiroSimoes commented Mar 17, 2022

Hello,

I'm currently building up a test suite for our drivers in lua. Right now I have the following problem, that I want to "mock" a global object (class? file?). My test currently requires my main .lua file, and I can succesfully test global variables. Now, that file contains the following codebit:

function CreateMessageParameters()
    return {
        [G_scRoom] = C4:ListGetRoomID(),
        [G_scC4Id] = C4:GetDeviceID(),
        [G_scAddress] = G_scDeviceAddress
    }
end

The C4 is not a library I have access to - so I can't really test it, but thats okay. I would want to make a test like this, in a different file, that has the file from above as required:

it("CreateMessageParameters should call the correct functions", function()
        C4 = { 
            ListGetRoomID = function() return "roomID" end,
            GetDeviceID = function() return "deviceID" end
        }

        stub(C4, "ListGetRoomID")
        stub(C4, "GetDeviceID")

        CreateMessageParameters()
        assert.stub(C4.ListGetRoomID).was.called()
        assert.stub(C4.GetDeviceID).was.called()

Now, of course, this doesn't work because attempt to index global 'C4' (a nil value). Is there a way to mock/stub this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant