美文网首页禅与计算机程序设计艺术
Go 接口嵌套组合的使用方法 & gomock 测试 stub

Go 接口嵌套组合的使用方法 & gomock 测试 stub

作者: 光剑书架上的书 | 来源:发表于2022-06-02 03:05 被阅读0次

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

    package rocket
    
    import (
        "code.byted.org/ecom/compass_data_index/driver"
        "code.byted.org/ecom/compass_data_index/service"
    )
    
    type IRocketFetcher interface {
        service.BasicInfoService
        driver.INavigatorDriver
    }
    
    type RocketFetcher struct {
        service.BasicInfoService
        driver.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 test
    
    format:
        find . -name '*.go' | xargs goimports -w
    
    test:
        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 driver
    
    mockgen_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.go
    
    mockgen_basic_info_service:
        mockgen -source=./service/basic_info_service.go -destination ./service/basic_info_service_mock.go -package service
    

    相关文章

      网友评论

        本文标题:Go 接口嵌套组合的使用方法 & gomock 测试 stub

        本文链接:https://www.haomeiwen.com/subject/yqwfmrtx.html