1

鉴于此结构和功能:

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

4

1 回答 1

2
  1. 你可以模拟接口的实现,比如
globalIndex
type Mock struct{}

func (m Mock) SomeFunc(){
    globalIndex++
}

func testA(t *testing.T) {
    a := ExampleModule{
        AnotherModule: Mock{},
    }
    a.A()
    assert(globalIndex == 1)
}

  1. 试试testifyAssertExpectations能帮你

https://github.com/stretchr/testify#mock-package

于 2019-03-20T02:59:19.023 回答