Skip to content

Latest commit

 

History

History
39 lines (28 loc) · 1.24 KB

README.md

File metadata and controls

39 lines (28 loc) · 1.24 KB

clock GoDoc Build Status codecov

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.

Usage

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()
}