duanqiang5722 2019-04-05 05:15
浏览 13
已采纳

如何在测试用例中修复“复合文字中的缺失类型”

I am trying to write test code for the function ReadField() but I am having difficulty defining the test cases.

It gives an error "missing type in composite literal". I believe its just some syntax error.

I have tried defining the struct outside the function body but it would still give the same error.

ReadField(string, string, bool) (bool, string)

func TestReadField(t *testing.T){

    testCases := []struct {
        Name    string
        Input   struct{ 
            FirstString     string 
            SecondString    string 
            SomeBool        bool
        }
        Expected struct{
            IsValid bool
            Message string
        }
    }{
        //This is where the error points to.

        //Valid 
        {"Test Case 1",

        //Missing type error 
        {"FirstString", "SecondString", true},

        //Missing type error 
        {true, "testMessage"},},

        //Same goes for the remaining

        {"Test Case 2", 
        {"FirstString", "SecondString", false},
        {true, "testMessage"},},

        {"Test Case 3", 
        {"FirstString", "SecondString", true},
        {false, "testMessage"},},
    }

    for _, testCase := range testCases{
        t.Run(testCase.Name, func(t *testing.T){
            isValid, message := ReadField(testCase.Input.FirstString, testCase.Input.SecondString, testCase.Input.SomeBool)
            if isValid != testCase.Expected.IsValid || message != testCase.Expected.Message {
                t.Errorf("Expected: %b, %b 
 Got: %b, %b", testCase.Expected.IsValid, testCase.Expected.Message, isValid, message)
            } else {
                t.Logf("Expected: %b, %b 
 Got: %b, %b", testCase.Expected.IsValid, testCase.Expected.Message, isValid, message)
            }
        })  
    }
}
  • 写回答

1条回答 默认 最新

  • dongyuan2652 2019-04-05 06:07
    关注

    As the error indicates, you need to include the type in your declaration. Since you're using anonymous types, that means you must repeat the type definition. This is, of course, super annoying:

        //Valid 
        {"Test Case 1",
    
        //Missing type error 
        struct{ 
            FirstString     string 
            SecondString    string 
            SomeBool        bool
        }{"FirstString", "SecondString", true},
    
       // etc ...
    

    So what you should do is either use named types:

    type testInput struct{ 
        FirstString     string 
        SecondString    string 
        SomeBool        bool
    }
    type expected struct{
        IsValid bool
        Message string
    }
    testCases := []struct {
        Name     string
        Input    testInput
        Expected expected
    }{
        //Valid 
        {"Test Case 1",
    
        //Missing type error 
        testInput{"FirstString", "SecondString", true},
    
        // etc ...
    

    Or (my preference), flatten your top-level struct, making everything far more readable:

    testCases := []struct {
        Name              string
        InputFirstString  string 
        InputSecondString string 
        InputSomeBool     bool
        IsValid           bool
        Message           string
    }{
        //Valid 
        {"Test Case 1",
        "FirstString",
        "SecondString",
        true,
     // etc...
    

    I also strongly encourage you to use field labels in your definition, to improve readability:

        //Valid 
        {
            Name:              "Test Case 1",
            InputFirstSTring:  "FirstString",
            InputSecondString: "SecondString",
            InputSomeBool:      true,
     // etc...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果