You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crate/wireexample/cmd/example_01/main.go

30 lines
842 B
Go

2 years ago
// 展示 wire 的两种 mock 实现
package main
import "fmt"
func main() {
approachA()
approachB()
}
// approach A:
// - 需要 mock 的对象定义成接口, 真正的实现和 mock 分别实现接口;
// - injector 函数使用 接口类型参数, 测试时传入 mock 对象;
func approachA() {
rander := &MockRander{}
guess := InjectGuessWithoutMock(rander)
rander.Value = 1
fmt.Println("approachA:", guess.Guess(10)) //nolint:gomnd
2 years ago
}
// approach B:
// - mock 和 真正的实现分别定义到两组 injector 中, injector 提供1个对象包含要测试的对象和 mock;
// - 测试时调用 mock injector 返回对象(包含要测试的对象和 mock);
func approachB() {
guessWithMock := InjectMockGuess()
guessWithMock.Mock.Value = 1
fmt.Println("approachB:", guessWithMock.Guess.Guess(10)) //nolint:gomnd
2 years ago
}