假设我们有这样一个方法:funcmethod(intrMyInterface){gointr.exec()}在单元测试method中,我们想断言inter.exec被调用了一次且仅调用了一次;所以我们可以在测试中用另一个模拟结构模拟它,这将为我们提供检查它是否被调用的功能:typemockInterfacestruct{CallCountint}func(m*mockInterface)exec(){m.CallCount+=1}在单元测试中:funcTestMethod(t*testing.T){varmockmockInterface{}method(mock)ifmock.Cal
假设我们有这样一个方法:funcmethod(intrMyInterface){gointr.exec()}在单元测试method中,我们想断言inter.exec被调用了一次且仅调用了一次;所以我们可以在测试中用另一个模拟结构模拟它,这将为我们提供检查它是否被调用的功能:typemockInterfacestruct{CallCountint}func(m*mockInterface)exec(){m.CallCount+=1}在单元测试中:funcTestMethod(t*testing.T){varmockmockInterface{}method(mock)ifmock.Cal