bytedance / mockey

a simple and easy-to-use golang mock library
Apache License 2.0
557 stars 22 forks source link

Please document conditional mock #5

Closed 17hao closed 1 year ago

17hao commented 1 year ago

How to use conditional mock in mockey?

Mock(Foo).When().Return().Build()
ycydsxy commented 1 year ago

please refer to mock_test.go, if you want to mock Fun only if the input argument equals a:

func Fun(a string) string {
    return a
}

you can do this:

Mock(Fun).When(func(a string) bool { return a == "a" }).Return("c").Build()

fmt.Println(Fun("a")) // "c", mocked
fmt.Println(Fun("b")) // "b", not mocked

We will improve relevant documentation in the future.