Skip to content

Commit

Permalink
[Test] Luassert make sure to revert function if it is already mocked.
Browse files Browse the repository at this point in the history
This commit overwrites luassert stub function to make sure that before
try to mock any function verifies that is not mocked, and if it is,
revert to the original one. This prevents scenarios where multiple stubs
are in one test and are leaked to other test and that scenario is hard
to debug/notice.

Signed-off-by: Eloy Coto <[email protected]>
  • Loading branch information
eloycoto committed May 30, 2019
1 parent 62b0d92 commit 0c61315
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions spec/luassert_helper.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
local busted = require('busted')
local assert = require('luassert.assert')
local spy = require('luassert.spy')
local stub = require('luassert.stub')
local util = require ('luassert.util')
local say = require('say')

-- The stub.new and spy.on functions are patched here to make sure that the
-- target function is not already a mocked function.
-- The main reason to do that is to avoid some test that a function is mocked
-- on the before_each and after in the test also is mocked again, all mocks
-- are reverted(state.revert in this module), but if the mock is double, it is
-- only reverted once, so the first mock function will be leaked to other test.
local original_stub_new = stub.new
local original_spy_on = spy.on

function stub.new(object, key, ...)
if object and spy.is_spy(object[key]) then
error("Stub for key='".. key .."' is already a mocked function, revert the parent function")
end
return original_stub_new(object, key, ...)
end

function spy.on(object, key)
if object and spy.is_spy(object[key]) then
error("Spy for key='".. key .."' is already a mocked function, revert the parent function")
end
return original_spy_on(object, key)
end

do -- set up reverting stubs
local state = require("luassert.state")

Expand Down
5 changes: 5 additions & 0 deletions spec/policy/3scale_batcher/3scale_batcher_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ describe('3scale batcher policy', function()
describe('and backend is available', function()
before_each(function()
local backend_client = require('apicast.backend_client')
backend_client.authorize:revert()
stub(backend_client, 'authorize').returns({ status = 200 })
end)

Expand All @@ -144,12 +145,14 @@ describe('3scale batcher policy', function()
describe('and backend is not available', function()
before_each(function()
local backend_client = require('apicast.backend_client')
backend_client.authorize:revert()
stub(backend_client, 'authorize').returns({ status = 500 })
end)

describe('and the authorization is in the downtime cache', function()
describe('and it is OK', function()
before_each(function()
batcher_policy.backend_downtime_cache.get:revert()
stub(batcher_policy.backend_downtime_cache, 'get').returns(200)
end)

Expand All @@ -163,6 +166,7 @@ describe('3scale batcher policy', function()

describe('and it is denied', function()
before_each(function()
batcher_policy.backend_downtime_cache.get:revert()
stub(batcher_policy.backend_downtime_cache, 'get').returns(409)
end)

Expand All @@ -182,6 +186,7 @@ describe('3scale batcher policy', function()

describe('and the authorization is not in the downtime cache', function()
before_each(function()
batcher_policy.backend_downtime_cache.get:revert()
stub(batcher_policy.backend_downtime_cache, 'get').returns(nil)
end)

Expand Down

0 comments on commit 0c61315

Please sign in to comment.