Clock is a library for mocking the time package in go. It supports mocking time.Timer and time.Ticker for testing purposes.
This library is heavily inspired by benbjohnson/clock and jonboulle/clockwork.
Instead of time
, use the clock.Clock
interface. In your real application, you can call clock.New()
to get the actual instance. For testing purposes, call clock.NewMock()
.
import (
"testing"
"github.com/leononame/clock"
)
func TestApp_Do(t *testing.T) {
clock := clock.NewMock()
context := Context{Clock: clock}
}
The interface exposes standard time
functions that can be used like usual.
import (
"time"
"github.com/leononame/clock"
)
func timer() {
c := clock.NewMock() // or clock.New()
t := c.NewTicker(time.Second)
now := c.Now()
}