当前位置: 代码迷 >> 综合 >> Go 接口嵌套组合的使用方法 gomock 测试 stub 代码生成
  详细解决方案

Go 接口嵌套组合的使用方法 gomock 测试 stub 代码生成

热度:90   发布时间:2023-09-11 05:30:51.0

Go 接口嵌套组合的使用方法

package rocketimport ("code.byted.org/ecom/compass_data_index/driver""code.byted.org/ecom/compass_data_index/service"
)type IRocketFetcher interface {service.BasicInfoServicedriver.INavigatorDriver
}type RocketFetcher struct {service.BasicInfoServicedriver.INavigatorDriver
}func NewRocketFetcher() *RocketFetcher {return &RocketFetcher{&service.BasicInfoServiceImpl{},&driver.NavigatorDriver{},}
}

gomock 测试 stub 代码生成

使用 -aux_files 指定内嵌接口的 pkg1=path1,pkg2=path2. 需要注意的是, pkg1 / pkg2 不能跟被测接口的包相同!
(may be mockgen's assumptions!)

-aux_files rocket=service/basic_info_service.go,rocket=driver/navigator_driver.go

一个 Makefile 的例子:

PROJECTNAME=$(shell basename "$(PWD)")
all: format testformat:find . -name '*.go' | xargs goimports -wtest:go test -v -cover ./...#test report
test_report:go test -v -cover -json ./...  | go-test-report -t "Test Report"#gomock
mockgen_navigator_driver:mockgen -source=./driver/navigator_driver.go -destination ./driver/navigator_driver_mock.go -package drivermockgen_rocket_fetcher:mockgen -source=./rocket/rocket_driver.go -destination ./rocket/rocket_driver_mock.go -package rocket -aux_files rocket=service/basic_info_service.go,rocket=driver/navigator_driver.gomockgen_basic_info_service:mockgen -source=./service/basic_info_service.go -destination ./service/basic_info_service_mock.go -package service