lunarmodules / luassert

Assertion library for Lua
MIT License
202 stars 77 forks source link

Update spy/stub to perform deep copy of arguments #100

Closed o-lim closed 9 years ago

o-lim commented 9 years ago

This fixes errors when called_with or returned_with fail if the spy/stub is called with table arguments that are modified after the call to the spy/stub. Similarly, this fixes errors when on_call_with is called with table arguments that are modified after the call to on_call_with.

it("checks called() and called_with() assertions", function()
  local s = spy.new(function() end)
  local t = { foo = { bar = { "test" } } }

  s(1, 2, 3)
  s("a", "b", "c")
  s(t)
  t.foo.bar = "value"  -- modify table after call to spy

  assert.spy(s).was.called(3)
  assert.spy(s).was.called_with(1, 2, 3)
  assert.spy(s).was.called_with("a", "b", "c")
  assert.spy(s).was.called_with({ foo = { bar = { "test" } } }) -- matches original table
  assert.spy(s).was_not.called_with(t) -- does not match modified table
end)

Note: This is on top of PR #96