mocknn

command module
v0.0.0-...-fb84462 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2022 License: MIT Imports: 3 Imported by: 0

README

mocknn

pkg.go.dev

mocknn is a mocking tool using -overlay option of go test.

This is experimental project. APIs may change, do not use your product development. If you try to use this tool, please give me your feedback by filing an issue.

How to install

$ go install github.com/tenntenn/mocknn@latest

How to use

For example, there is a .go file which is test target.

// simple.go: test target
package simple

import "fmt"

const msg string = "hello"

func New(n int) *T {
	return &T{n: n}
}

type T struct {
	n int
}

func (t *T) V() int {
	return t.n
}

func F(t *T) {
	fmt.Println(msg, t.V())
}

simple_test.go is a test file for simple.go.

package simple_test

import (
	"testing"

	"github.com/tenntenn/mocknn/_example/simple"
)

func Test(t *testing.T) {
	v := simple.New(10)
	simple.F(v)
}

mock_test.go provides mockings for simple.go.

package simple

//mocknn: msg
const msgMock = "mock"

//mocknn: T
type MockT struct {
	m int
}

//mocknn: New
func MockNew(m int) *MockT {
	return &MockT{m: m * 2}
}

func (t *MockT) V() int {
	return t.m
}

mocknn replaces implementation to mocking which has //mocknn: comment directive. mocknn uses -overlay option to replace mocking files.

# use original implementation
$ go test
hello 10
PASS
ok  	github.com/tenntenn/mocknn/_example/simple	0.359s

# use mockings
$ go test -overlay=`mocknn`
mock 20
PASS
ok  	github.com/tenntenn/mocknn/_example/simple	0.273s

# same as go test -overlay=`mocknn`
$ mocknn test
mock 20
PASS
ok  	github.com/tenntenn/mocknn/_example/simple	0.273s

mocknn can work with testtime.

$ cd _example/timesample
$ mocknn test -overlay=`testtime`
2006-01-02 03:00:00 +0000 UTC
PASS
ok  	github.com/tenntenn/mocknn/_example/timesample	0.837s

Examples

See _example directory.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL