lunarmodules / luassert

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

Add mock revert #85

Closed o-lim closed 9 years ago

o-lim commented 9 years ago

This allows mocks to be easily reverted to their original state.

describe("mocks", function()
  local t = {
    thing = function(msg) print(msg) end
  }

  after_each(function()
    mock.revert(t)  -- restores table to original state
  end)

  it("replaces a table with spies", function()
    local m = mock(t) -- mocks the table with spies, so it will print

    m.thing("Coffee")
    assert.spy(m.thing).was.called_with("Coffee")
  end)

  it("replaces a table with stubs", function()
    local m = mock(t, true) -- mocks the table with stubs, so it will not print

    m.thing("Coffee")
    assert.stub(m.thing).was.called_with("Coffee")
  end)
end)

Fixes issue #59.

o-lim commented 9 years ago

Updated mock.revert to return the original object. This makes it consistent with stub.revert and spy.revert, which will also return the original object.