gstub

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

Golang 单元测试工具类库,替换变量值为测试对象打桩。配同 gomock 使用。

接口说明

配合 Gomock 使用

假如你有如下定义的依赖

type OneServer interface {
	Serve(ctx context, req interface{}, rsp interface{}) error
}
type Servers struct {
	One OneServer 
}

var svrs *Servers
func GetServers() *Servers {
	return svrs
}

在测试时

ctrl := gomock.NewControler(t)
stub := gstub.New(nil)
defer func() {
    stub.Reset()
    ctrl.Finish()
}

mock := mockpkg.NewOneServer(ctrl)
mock.EXPECT().Serve(gomock.Any(), gomock.Eq(xxx), gstub.Set(xxx)).Return(nil)
stub.Path(GetServes(), "One", mock)

// do logic

Var 替换单个变量

v := &Person{}
defer gostub.Var(v, Person{}).Reset()

FieldByName、Field 替换结构体字段的值

v := &Person{}
defer gostub.FieldByName(v, "name", "new name").Reset()
defer gostub.Field(v, 0, "new name").Reset()

Elem 替换切片或者数组的元素值

arr := []*Person{{}}
defer gostub.Elem(arr, 0, &Person{}).Reset()
arr := [1]*Person{{}}
defer gostub.Elem(&arr, 0, &Person{}).Reset()

Map 替换映射中的某个键的值

m := map[string]*Person{"":nil}
defer gostub.Map(m, "", &Person{}).Reset()

Path 根据索引替换深层值

root := &Person{}
defer gostub.Path(root, "字段名 数组切片下标 映射字符串或数字键 以点分割", "replaced content").Reset()

FuncOuts 替换函数变量以固定次数返回值代替

fn := func()string{ return "" }
defer gostub.FuncOuts(&fn, gostub.OutValues)

Method 替换接口变量的方法返回值

var c Client = new(client)
stub := gostub.InterfaceMethod(reflect.ValueOf(&c).Elem(), "method name", &Forward{}, gostub.OutValues)

xxxNext 沿用上次替换的 target,含义同上

Reset 还原该 stub 的所有替换值

详细样例参见项目单测

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Set

func Set(v interface{}) gomock.Matcher

Set 设置入参值,该入参类型必须为指针或接口型。

func Version added in v1.0.3

func Version() string

Version gdat 语义化版本号。

Types

type Forwarder

type Forwarder interface {
	// Set methodName 接口方法名,newFunc 新的将被回调的接口方法,originalTarget 原接口内部动态值。
	Set(methodName string, newFunc interface{}, originalTarget interface{})
}

Forwarder 接口方法替换调用。

type OutValue

type OutValue []interface{}

type OutValues

type OutValues []struct {
	Values OutValue
	Times  int
}

type Stub

type Stub interface {
	// Var 替换单个变量。
	Var(target interface{}, substitute interface{}) Stub
	// FuncOuts 替换函数变量以固定次数返回值代替。
	FuncOuts(target interface{}, outs OutValues) Stub
	// FieldByName 替换结构体字段的值。
	FieldByName(target interface{}, name string, substitute interface{}) Stub
	// Field 替换结构体字段的值。
	Field(target interface{}, index int, substitute interface{}) Stub
	// Elem 替换切片或者数组的元素值。
	Elem(target interface{}, index int, substitute interface{}) Stub
	// Map 替换映射中的某个键的值。
	Map(target interface{}, key interface{}, substitute interface{}) Stub
	// Path 根据索引替换深层值。
	Path(target interface{}, path string, substitute interface{}) Stub
	// PathByList 根据索引替换深层值。
	PathByList(target interface{}, list []string, substitute interface{}) Stub
	// Method 替换接口变量的方法返回值。
	Method(target reflect.Value, method string, substitute Forwarder, outs OutValues) Stub

	// FieldNext 沿用上次替换的 target,含义同上。
	FieldNext(index int, substitute interface{}) Stub
	// FieldByNameNext 沿用上次替换的 target,含义同上。
	FieldByNameNext(name string, substitute interface{}) Stub
	// ElemNext 沿用上次替换的 target,含义同上。
	ElemNext(index int, substitute interface{}) Stub
	// MapNext 沿用上次替换的 target,含义同上。
	MapNext(key interface{}, substitute interface{}) Stub
	// FieldFuncOutsNext 沿用上次替换的 target,含义同上。
	FieldFuncOutsNext(index int, outs OutValues) Stub
	// FieldFuncOutsByNameNext 沿用上次替换的 target,含义同上。
	FieldFuncOutsByNameNext(name string, outs OutValues) Stub

	// ToElem 打桩成 Elem。
	ToElem(index int) Stub
	// ToField 打桩成字段。
	ToField(index int) Stub
	// ToFieldByName 打桩成字段。
	ToFieldByName(name string) Stub
	// ToMapVal 打桩成 map 值。
	ToMapVal(key interface{}) Stub

	// Reset 还原该 stub 的所有替换值。
	Reset()
}

Stub 打桩对象能力定义。

func Elem

func Elem(target interface{}, index int, substitute interface{}) Stub

Elem 替换切片或者数组的元素值。

func Field

func Field(target interface{}, index int, substitute interface{}) Stub

Field 替换结构体字段的值。

func FieldByName

func FieldByName(target interface{}, name string, substitute interface{}) Stub

FieldByName 替换结构体字段的值。

func FuncOuts

func FuncOuts(target interface{}, outs OutValues) Stub

FuncOuts 替换函数变量以固定次数返回值代替。

func Map

func Map(target interface{}, key interface{}, substitute interface{}) Stub

Map 替换映射中的某个键的值。

func Method

func Method(target reflect.Value, method string, substitute Forwarder, outs OutValues) Stub

Method 替换接口变量的方法返回值。

func New

func New(target interface{}) Stub

New 分配一个新的打桩对象。

func Path

func Path(target interface{}, path string, substitute interface{}) Stub

Path 根据索引替换深层值。

func Var

func Var(target interface{}, substitute interface{}) Stub

Var 替换单个变量。

Jump to

Keyboard shortcuts

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