Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Fix test code Error: An error is expected but got nil. Test: TestCrElasticRan_Validate/…

  1. User

    Fix test code Error: An error is expected but got nil. Test: TestCrElasticRan_Validate/```crElasticRan_integration_request_with_empty_Celldata // TestCrElasticRan_Validate tests *model.CrElasticRan.Validate func TestCrElasticRan_Validate(t *testing.T) {

    //init test cases
    var cases []CrElasticRanTest = []CrElasticRanTest{
    	{
    		Name: "valid crElasticRan integration request",
    		Data: func() *CrElasticRan {
    			return TestCrElasticRan(t)
    		},
    		IsValid: true,
    	},
    	{
    		Name: "crElasticRan integration request with empty Name",
    		Data: func() *CrElasticRan {
    			data := TestCrElasticRan(t)
    			data.ENodeB.Site.Cellular.Name = ""
    			return data
    		},
    		IsValid: false,
    	},
    	{
    		Name: "crElasticRan integration request with empty Celldata",
    		Data: func() *CrElasticRan {
    			data := TestCrElasticRan(t)
    			data.EUtranCellData.CellsGroups = nil
    			return data
    		},
    		IsValid: false,
    	},
    //run tests
    for _, test := range cases {
    	t.Run(test.Name, func(t *testing.T) {
    		if test.IsValid {
    			assert.NoError(t, test.Data().Validate())
    		} else {
    			assert.Error(t, test.Data().Validate())
    		}
    	})
    }
    

    }```

  2. Assistant

    To fix the test code, you need to close the var cases []CrElasticRanTest slice declaration before running the tests. Here's the corrected code:

    // TestCrElasticRan_Validate tests *model.CrElasticRan.Validate
    func TestCrElasticRan_Validate(t *testing.T) {
    
    	//init test cases
    	var cases []CrElasticRanTest = []CrElasticRanTest{
    		{
    			Name: "valid crElasticRan integration request",
    			Data: func() *CrElasticRan {
    				return TestCrElasticRan(t)
    			},
    			IsValid: true,
    		},
    		{
    			Name: "crElasticRan integration request with empty Name",
    			Data: func() *CrElasticRan {
    				data := TestCrElasticRan(t)
    				data.ENodeB.Site.Cellular.Name = ""
    				return data
    			},
    			IsValid: false,
    		},
    		{
    			Name: "crElasticRan integration request with empty Celldata",
    			Data: func() *CrElasticRan {
    				data := TestCrElasticRan(t)
    				data.EUtranCellData.CellsGroups = nil
    				return data
    			},
    			IsValid: false,
    		},
    	}
    
    	//run tests
    	for _, test := range cases {
    		t.Run(test.Name, func(t *testing.T) {
    			if test.IsValid {
    				assert.NoError(t, test.Data().Validate())
    			} else {
    				assert.Error(t, test.Data().Validate())
    			}
    		})
    	}
    }
    
  3. User

    Error is in one test case crElasticRan integration request with empty Celldata

  4. Assistant

    To fix the error in the test case "crElasticRan integration request with empty Celldata", you need to ensure that the CellsGroups field is empty instead of nil.

    Here's the corrected code:

    // TestCrElasticRan_Validate tests *model.CrElasticRan.Validate
    func TestCrElasticRan_Validate(t *testing.T) {
    
    	//init test cases
    	var cases []CrElasticRanTest = []CrElasticRanTest{
    		{
    			Name: "valid crElasticRan integration request",
    			Data: func() *CrElasticRan {
    				return TestCrElasticRan(t)
    			},
    			IsValid: true,
    		},
    		{
    			Name: "crElasticRan integration request with empty Name",
    			Data: func() *CrElasticRan {
    				data := TestCrElasticRan(t)
    				data.ENodeB.Site.Cellular.Name = ""
    				return data
    			},
    			IsValid: false,
    		},
    		{
    			Name: "crElasticRan integration request with empty Celldata",
    			Data: func() *CrElasticRan {
    				data := TestCrElasticRan(t)
    				data.EUtranCellData.CellsGroups = []CellGroup{} // set as empty slice
    				return data
    			},
    			IsValid: false,
    		},
    	}
    
    	//run tests
    	for _, test := range cases {
    		t.Run(test.Name, func(t *testing.T) {
    			if test.IsValid {
    				assert.NoError(t, test.Data().Validate())
    			} else {
    				assert.Error(t, test.Data().Validate())
    			}
    		})
    	}
    }
    

    In the "crElasticRan integration request with empty Celldata" test case, I updated the line data.EUtranCellData.CellsGroups = nil to data.EUtranCellData.CellsGroups = []CellGroup{} to ensure that the field is empty.

  5. User

    CellGroups is map[string]*Eutrancells

  6. Assistant

    Apologies for the confusion. Since CellsGroups is a map with string keys and *Eutrancells values, you should assign an empty map to CellsGroups instead of nil.

    Here's the corrected code for the "crElasticRan integration request with empty Celldata" test case:

    // TestCrElasticRan_Validate tests *model.CrElasticRan.Validate
    func TestCrElasticRan_Validate(t *testing.T) {
    
    	//init test cases
    	var cases []CrElasticRanTest = []CrElasticRanTest{
    		{
    			Name: "valid crElasticRan integration request",
    			Data: func() *CrElasticRan {
    				return TestCrElasticRan(t)
    			},
    			IsValid: true,
    		},
    		{
    			Name: "crElasticRan integration request with empty Name",
    			Data: func() *CrElasticRan {
    				data := TestCrElasticRan(t)
    				data.ENodeB.Site.Cellular.Name = ""
    				return data
    			},
    			IsValid: false,
    		},
    		{
    			Name: "crElasticRan integration request with empty Celldata",
    			Data: func() *CrElasticRan {
    				data := TestCrElasticRan(t)
    				data.EUtranCellData.CellsGroups = make(map[string]*Eutrancells) // set as empty map
    				return data
    			},
    			IsValid: false,
    		},
    	}
    
    	//run tests
    	for _, test := range cases {
    		t.Run(test.Name, func(t *testing.T) {
    			if test.IsValid {
    				assert.NoError(t, test.Data().Validate())
    			} else {
    				assert.Error(t, test.Data().Validate())
    			}
    		})
    	}
    }
    

    In the "crElasticRan integration request with empty Celldata" test case, I updated the line data.EUtranCellData.CellsGroups = []CellGroup{} to data.EUtranCellData.CellsGroups = make(map[string]*Eutrancells) to assign an empty map to CellsGroups.