我试图将两个对象等同起来,以检查我的单元测试,并将两个对象等同起来,我使用reflect.DeepEqual函数,但reflect.DeepEqual返回false,即使输出和预期对象相等
下面是我的结构:
type Packages struct {
AppGroup string `json:"appGroup"`
AppManifest string `json:"appManifest"`
CmdArgs string `json:"cmdArgs"`
FileName string `json:"fileName"`
ImageList []string `json:"imageList"`
ImageNames []string `json:"imageNames"`
Name string `json:"name"`
Requires []string `json:"requires"`
Source string `json:"source"`
StartUp string `json:"startUp"`
AppCategory string `json:"appCategory"`
Type string `json:"type"`
Version string `json:"version"`
}
EmlStartupManifest()方法如下:
var EmlStartupManifest = func() ([]model.Packages, error) {
data := model.EMMODEL{}
packageData := []model.Packages{}
yamlFile, err := FileReadEmlStartupManifest()
if err != nil {
return packageData, err
}
j, err := yaml.YAMLToJSON(yamlFile)
if err != nil {
return packageData, err
}
json.Unmarshal([]byte(string(j)), &data)
for _, installer := range data.Installers {
if installer.Name == provider.INSTALLER_NAME {
packageData = installer.Packages
}
}
return packageData, nil
}
这是我的测试方法:
var emlStartupManifest_output2 = new([]model.Packages)
func Test_EmlStartupManifest(t *testing.T) {
tests := []struct {
name string
expected []model.Packages
mockFunc func()
expectingErr bool
}{
{
name: "Check EML Startup Manifest error with non-existent file",
expected: *emlStartupManifest_output2,
mockFunc: func() {
FileReadEmlStartupManifest = func() ([]byte, error) {
yamlFile, err := ioutil.ReadFile("../test/data/dummy.yaml")
return yamlFile, err
}
},
expectingErr: true,
},
{
name: "Check EML Startup Manifest error with corrupted yaml file",
expected: *emlStartupManifest_output2,
mockFunc: func() {
FileReadEmlStartupManifest = func() ([]byte, error) {
yamlFile, err := ioutil.ReadFile("../test/data/corrupt.yaml")
return yamlFile, err
}
},
expectingErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(tt *testing.T) {
tc.mockFunc()
output, err := EmlStartupManifest()
eq := reflect.DeepEqual(output, tc.expected)
log.Println(eq)
fmt.Printf("Output: %v Expected: %v %v %v\n", output, tc.expected, reflect.TypeOf(output), reflect.TypeOf(tc.expected))
if !eq {
t.Errorf("Curr tc: %v", tc.name)
t.Errorf("Output %q not equal to expected %q", output, tc.expected)
}
if err != nil && tc.expectingErr == false {
t.Errorf("Curr tc: %v", tc.name)
t.Errorf("not expected error but got the error")
}
if err == nil && tc.expectingErr == true {
t.Errorf("Curr tc: %v", tc.name)
t.Errorf("expected error but did not get the error")
}
})
}
}
即使输出和预期是相同的Deepequal返回false。下面是我的输出:
有谁能告诉我为什么会发生这种情况,以及可能的解决方案是什么?.....谢谢!
1条答案
按热度按时间amrnrhlw1#
我猜你是在比较一个
nil
切片和一个空切片。他们看起来一样,但不被认为是一样的。