鉴于此结构和功能:
type ExampleModule struct {
DB *database.Store
AnotherModule AnotherModuleInterface
}
func(m *ExampleModule) A (i int, id int[]) error{
err := m.AnotherModuke.SomeFunc(i, id)
}
我怎样才能进行单元测试以确保在运行SomeFunc
函数时调用A
?
globalIndex
type Mock struct{}
func (m Mock) SomeFunc(){
globalIndex++
}
func testA(t *testing.T) {
a := ExampleModule{
AnotherModule: Mock{},
}
a.A()
assert(globalIndex == 1)
}
testify
。 AssertExpectations
可以帮助你