Skip to content

Commit

Permalink
Reset spies (#127)
Browse files Browse the repository at this point in the history
* reset spy

* wrong condition
  • Loading branch information
corinapurcarea committed May 11, 2021
1 parent 4449f40 commit 4d3ba56
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/main/lua/com/adobe/test/framework/Mocka.lua
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ function spy(class, method, fn)
return mapObj
end

function reset_spy(class, method)
if spies and spies[class] then
spies[class][method] = mirror[class][method]
end

if lazy_spies and lazy_spies[class] then
lazy_spies[class][method] = mirror[class][method]
end
end

---
-- @param path {string} - path to require
Expand Down
15 changes: 15 additions & 0 deletions src/test/adobe/mocka/spyTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@ test('override lazy spy', function()

local cjson = require "cjson"
assertEquals(cjson.decode(), "overwritten")

reset_spy("cjson", "decode")

local payload = '{"key":"test"}'
local decoded = {}
decoded["key"] = "test"

local cjson = require "cjson"
assertEquals(cjson.decode(payload), decoded)
end)

test('override preloaded module spy', function()
spy("src.test.adobe.mocka.some_class", "testing", function()
return "overwritten preloaded module"
end)
assertEquals(some_class:testing(), "overwritten preloaded module")

reset_spy("src.test.adobe.mocka.some_class", "testing")
assertEquals(some_class:testing(), "real")
end)

test('just beforeEach on preload', function()
assertEquals(some_class:testing(), "beforeEach")

reset_spy("src.test.adobe.mocka.some_class", "testing")
assertEquals(some_class:testing(), "real")
end)
21 changes: 18 additions & 3 deletions src/test/adobe/mocka/spyTestNginx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,37 @@ afterEach(function()
isNgx = false
end)

test('override lazy spy', function()
test('nginx - override lazy spy nginx', function()
spy("cjson", "decode", function()
return "overwritten"
end)

local cjson = require "cjson"
assertEquals(cjson.decode(), "overwritten")

reset_spy("cjson", "decode")

local payload = '{"key":"test"}'
local decoded = {}
decoded["key"] = "test"

local cjson = require "cjson"
assertEquals(cjson.decode(payload), decoded)
end)

test('override preloaded module spy', function()
test('nginx - override preloaded module spy nginx', function()
spy("src.test.adobe.mocka.some_class", "testing", function()
return "overwritten preloaded module"
end)
assertEquals(some_class:testing(), "overwritten preloaded module")

reset_spy("src.test.adobe.mocka.some_class", "testing")
assertEquals(some_class:testing(), "real")
end)

test('just beforeEach on preload', function()
test('nginx - just beforeEach on preload nginx', function()
assertEquals(some_class:testing(), "beforeEach")

reset_spy("src.test.adobe.mocka.some_class", "testing")
assertEquals(some_class:testing(), "real")
end)

0 comments on commit 4d3ba56

Please sign in to comment.