britzl / deftest

Unit testing in Defold

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to mock `sys`

unindented opened this issue · comments

I've cloned the repo, and added the following test:

diff --git a/test/test_mock.lua b/test/test_mock.lua
index 424fdbd..12c7a0d 100644
--- a/test/test_mock.lua
+++ b/test/test_mock.lua
@@ -111,5 +111,14 @@ return function()
 			assert(m.fn1() == "foo")
 			assert(m.fn2() == "bar")
 		end)
+
+		it("should be able to mock sys", function()
+			mock.mock(sys)
+			sys.get_sys_info.returns({ language = "zu" })
+			pprint(sys.get_sys_info())
+			assert(sys.get_sys_info().language == 'zu') -- FAILS HERE
+			mock.unmock(sys)
+			assert(sys.get_sys_info().language == 'es')
+	end)
 	end)
 end

When I run .test/run.sh, the pprint call prints the following (which suggests the mock didn't do anything):

DEBUG:SCRIPT:
{ --[[0x10a34d130]]
  device_ident = "",
  user_agent = "",
  device_model = "",
  manufacturer = "",
  system_name = "Darwin",
  system_version = "21.6.0",
  api_version = "",
  device_language = "es",
  territory = "US",
  language = "es",
  gmt_offset = -420
}

And I get the following test failure:

DEBUG:SCRIPT: --- ERROR REPORT ------------------
DEBUG:SCRIPT: should be able to mock sys:
DEBUG:SCRIPT: deftest/telescope.lua:233: assertion failed!
DEBUG:SCRIPT: stack traceback:
DEBUG:SCRIPT: 	[C]: in function 'assert'
DEBUG:SCRIPT: 	deftest/telescope.lua:233: in function 'assert'
DEBUG:SCRIPT: 	test/test_mock.lua:118: in function <test/test_mock.lua:115>
DEBUG:SCRIPT: 	[C]: in function 'xpcall'
DEBUG:SCRIPT: 	deftest/telescope.lua:419: in function 'invoke_test'
DEBUG:SCRIPT: 	deftest/telescope.lua:459: in function 'run'
DEBUG:SCRIPT: 	deftest/deftest.lua:75: in function <deftest/deftest.lua:73>
DEBUG:SCRIPT: stack traceback:
DEBUG:SCRIPT: 	deftest/telescope.lua:427: in function 'invoke_test'
DEBUG:SCRIPT: 	deftest/telescope.lua:459: in function 'run'
DEBUG:SCRIPT: 	deftest/deftest.lua:75: in function <deftest/deftest.lua:73>
DEBUG:SCRIPT: Generating code coverage report
DEBUG:SCRIPT:   Skipping:	builtins/render/default.render_script
DEBUG:SCRIPT:   Processing:	deftest/deftest.lua
DEBUG:SCRIPT:   Processing:	deftest/mock/fs.lua
DEBUG:SCRIPT:   Processing:	deftest/mock/gui.lua
DEBUG:SCRIPT:   Processing:	deftest/mock/mock.lua
DEBUG:SCRIPT:   Processing:	deftest/mock/time.lua
DEBUG:SCRIPT:   Processing:	deftest/telescope.lua
DEBUG:SCRIPT:   Processing:	deftest/util/check.lua
DEBUG:SCRIPT:   Processing:	deftest/util/unload.lua
DEBUG:SCRIPT:   Skipping:	remove

  • Platform is x86_64-darwin (macOS Monterey 12.5)
  • dmengine_headless is version 1.3.4 (80b1b73)
  • bob.jar is version 1.3.4 (80b1b73)

Ah, ok, the documentation might be lacking here. The returns() function expects a table with a list of return values which will be returned one value at a time when the function is called. Example:

mock.mock(math)
math.random.returns(1, 100, 5, 10)

assert(math.random() == 1)
assert(math.random() == 100)
assert(math.random() == 5)
assert(math.random() == 10)

There is also a always_returns() mock function:

mock.mock(math)
math.random.always_returns(1234)

assert(math.random() == 1234)
assert(math.random() == 1234)
assert(math.random() == 1234)
assert(math.random() == 1234)

Finally there is the replace() function:

mock.mock(math)
math.random.replace(function(m, n) return m or 0 end)

assert(math.random(1,100) == 1)
assert(math.random(200) == 200)
assert(math.random() == 0)

So in your case you either want always_returns or wrap your sys_info table in another table:

			sys.get_sys_info.returns({
				{ language = "se" },	-- returned first time sys.get_sys_info()) is called
				{ language = "zu" },	-- returned second time sys.get_sys_info()) is called				
			})

Aaah, understood. Thank you so much for explaining!