The package expect-spy
is a plugin to expect which adds words used
to manage luassert spies.
You can install expect-spy
using LuaRocks with the command:
luarocks install expect-spy
In order to use the plugin, you must declare it somewhere in your tests. A good place for this is a file
always read before executing the tests. For that, simply require the module, providing the expect
object as
parameter.
local expect = require('expect')
require('expect-spy')(expect)
If count
is not provided, asserts that the target spy was called at least once. If count
is provided,
asserts that the target spy was called exactly the given count of times.
local s = spy.new(function() end)
s(1, 2, 3)
s(4, 5)
expect(s).to.have.been.called() -- Called at least once
expect(s).to.have.been.called(2) -- Called exactly twice
Asserts that the target spy has been called at least once with the given arguments.
local s = spy.new(function() end)
s(1, 2, 3)
s(4, 5)
expect(s).to.have.been.calledWith(4, 5)
expect(s).to.have.been.calledWith(1, 2, 3)
Asserts that the target spy returned at least once the given values.
local s = spy.new(function() return 42 end)
s()
expect(s).to.have.returned(42)