CFC-Servers / GLuaTest

An exciting and nimble unit testing framework for GLua projects
https://discord.gg/epJK9Xx3pc
GNU General Public License v3.0
51 stars 2 forks source link

Enhance async env, give env to beforeEach calls #15

Closed brandonsturgeon closed 2 years ago

brandonsturgeon commented 2 years ago

Stubs won't play nicely in async environments (or at least, I can't work out how to do it), so I intentionally left out stubs from Async.

I'll add them here, but the onus is on the user to make sure their async tests aren't using conflicting stubs (remember, async tests run as a batch per-file, so as long as all of the tests in a single file play nicely, it should be fine.)

This also solves #14 by allowing beforeEach (but not currently beforeAll) to create and use stub()

This PR was tested with a full set of synchronous tests, and I tested the async functionality with this:

        {
            name = "Async test",
            async = true,
            timeout = 2,
            func = function()
                local og = function() return "test1" end

                local var = {
                    func = og
                }

                local funcStub = stub( var, "func" ).returns( "test2" )

                timer.Simple( 1, function()
                    expect( var.func() ).to.equal( "test2" )
                    expect( funcStub ).to.haveBeenCalled()

                    funcStub:Restore()
                end )

                timer.Simple( 1.5, function()
                    expect( var.func ).to.equal( og )
                    done()
                end )
            end
        }