Fix test code Error: An error is expected but got nil. Test: TestCrElasticRan_Validate/…
- 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()) } }) }}```
- Assistant
To fix the test code, you need to close the
var cases []CrElasticRanTestslice 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()) } }) } } - User
Error is in one test case crElasticRan integration request with empty Celldata
- Assistant
To fix the error in the test case "crElasticRan integration request with empty Celldata", you need to ensure that the
CellsGroupsfield is empty instead ofnil.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 = niltodata.EUtranCellData.CellsGroups = []CellGroup{}to ensure that the field is empty. - User
CellGroups is map[string]*Eutrancells
- Assistant
Apologies for the confusion. Since
CellsGroupsis a map withstringkeys and*Eutrancellsvalues, you should assign an empty map toCellsGroupsinstead ofnil.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{}todata.EUtranCellData.CellsGroups = make(map[string]*Eutrancells)to assign an empty map toCellsGroups.