【问题标题】:How to unit test a Cobra CLI command which requires internal mocks?如何对需要内部模拟的 Cobra CLI 命令进行单元测试?
【发布时间】:2021-03-13 01:12:49
【问题描述】:

我已经定义了以下 Cobra 子命令,我想使用 testify 来测试输出:

var childCmd = &cobra.Command{
    Args:  cobra.MinimumNArgs(1),
    RunE: func(cmd *cobra.Command, args []string) error {
        id := args[0]
        conn := getConn()
        c := newClient(conn)

        out, err := c.getResult(cmd.Context(), id)
        if err != nil {
            return err
        }

        fmt.Printf(
            "Name:\t%v %v\nCity:\t%v\n",
            out.GetFirstName().String(),
            out.GetLastName().String(),
            out.GetCity().String(),
        )

        return nil
    },
}

func init() {
    rootCmd.AddCommand(childCmd)
}

我可以用这样的方式测试实际输出:

func executeCommand(root *cobra.Command, args ...string) (output string, err error) {
    buf := new(bytes.Buffer)
    root.SetOut(buf)
    root.SetErr(buf)
    root.SetArgs(args)

    err = root.Execute()
    if err != nil {
        fmt.Println(err)
    }

    return buf.String(), err
}

func TestGetResult(t *testing.T) {
    rootCmd.AddCommand(childCmd)
    output, _ := executeCommand(rootCmd, "child", "1")
    assert.Equal(t, "test", output)
}

但我缺少的是如何模拟 gRPC 客户端及其对getResult 的相应调用。 有没有比在RunE func 中创建新的 gRPC 客户端更好的方法来帮助模拟?截至目前,单元测试尝试连接到不存在的 gRPC 客户端并失败。

【问题讨论】:

    标签: unit-testing go command-line-interface testify cobra


    【解决方案1】:

    一种选择是在单元测试中使用随机端口运行 gRPC 服务器,并让客户端连接到该端口。如果您的树中也有 gRPC 服务器,并且它不会引入您无法模拟的其他东西,那么这是可能的。

    更简单的选择是将 gRPC 初始化提取到函数变量,并在单元测试期间将其设置为返回模拟:

    // Get connection and return client
    func GetGRPCClient() (MyClient,error) {
      ...
    }
    
    var getClient = GetGRPCClient
    
    var childCmd = &cobra.Command{
      // use getClient in the command implementation
    }
    
    
    ***_test.go:
    
    func getTestGRPCClient() (MyClient,error) {
      // Construct a mock client and return it
    }
    
    func TestCommand(t *testing.T) { 
       getClient=getTestGRPCClient
       // Run the test
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 2019-08-22
      相关资源
      最近更新 更多