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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)