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 OOP objects #629

Open
software-natives-docker opened this issue Apr 28, 2020 · 1 comment
Open

Mocking OOP objects #629

software-natives-docker opened this issue Apr 28, 2020 · 1 comment

Comments

@software-natives-docker

I wonder whether there's a better way to use busted in the following use case.

I'm using OOP as outlined here: http://lua-users.org/wiki/ObjectOrientationTutorial. Googling for OOP (object oriented, etc.) along with busted didn't yield any useful results, thus this issue.

I've got this class (production code):

local McModule = {}
McModule.__index = McModule
setmetatable(
    McModule,
    {
        __call = function(cls, ...)
            local self = setmetatable({}, cls)
            self:_init(...)
            return self
        end
    }
)

function McModule:_init()
    self.fcts = {}
end

function McModule:AddCommand(fct, name)
    self.fcts[name] = fct
end

function McModule:GetCommand(name)
    return self.fcts[name]
end

return McModule

I'm trying to mock functions and assert on the calls being made. Here's my test code

describe(
    'INCO API',
    
    function()
        local n, m, snapshot

        before_each(
            function()
                snapshot = assert:snapshot()
                m = mock(_G.MI.Mc)
                n = mock(getmetatable(_G.MI.Mc))
                -- ensure module is actually (re)loaded
                package.loaded['SHSSIStripHandlerSeq_06_StripHandling'] = nil
            end
        )

        after_each(
            function()
                snapshot:revert()
                -- Using these instead of snapshot:reverts() causes a "hanger" (program never ends, CPU 0%)
                -- mock.revert(m)
                -- mock.revert(n)
            end
        )

        it(
            'App.LoadStripWireDispense',
            function()
                require 'SHSSIStripHandlerSeq_06_StripHandling'

                assert.stub(n.AddCommand).was.called(4)
                assert.stub(n.AddCommand).was.called_with(match.is_ref(m), match.is_function(), 'App.LoadStripWireDispense')
                assert.stub(n.AddCommand).was.called_with(match.is_ref(m), match.is_function(), 'App.LoadStripPrePress')
                assert.stub(n.AddCommand).was.called_with(match.is_ref(m), match.is_function(), 'App.LoadStripInspect', match.is_table())
                assert.stub(n.AddCommand).was.called_with(match.is_ref(m), match.is_function(), 'App.LoadStripUnload', match.is_table())
            end
        )
    end
)

There are a couple of questions:

  • Is there a better way of mocking OOP objects instead of mocking the metatable?
  • Is there a better way instead of having both m and n?
  • Why does mock.revert(n) mock.revert(m) hang but snapshot:revert() seems to work
@mecampbellsoup
Copy link

Perhaps related but, is there a way to mock the return value of some mocked function? How are people mocking 3rd party modules that are integrated with their own code if not?

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

2 participants