测试是否调用了函数

时间:2019-03-20 02:44:05

标签: go testing gomega

给出以下结构和功能:

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

1 个答案:

答案 0 :(得分: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

相关问题