douchuifk90315 2019-01-11 04:06
浏览 122
已采纳

为什么使用reflect.deepEqual导致该测试失败?

I have the following struct with getters for every field

```
type Profile struct {
    id        dom.ID
    screeName ScreenName
    dob       DOB
    gender    Gender
    bio       Bio
    avatar    Avatar
    company   Company
    website   Website
    address   address.Address
    accounts  []dom.ID
}
```

the types for each field are just wrappers around strings that do some validation or whatever. The getters are all the same format,

```
// ScreenName returns the ScreenName for the profile
func (p *Profile) ScreenName() ScreenName {
    return p.screeName
}

// DOB returns the dob for the profile
func (p *Profile) DOB() DOB {
    return p.dob
}
```

the constructor function is below, in the absence of an option being provided to set the struct fields a random default value is provided for it.

```
type Option func(prof *Profile)

func New(opts ...Option) *Profile {
    p := newWithAllDefaults()
    for _, opt := range opts {
        opt(p)
    }
    return p
}
```

All of the Option types that can be passed to the constructor are tested and setting non-exported fields on the Profile struct.

Now the issue that I have run into is with testing. I have only been using go for maybe 3 months now so I am sure that there's something that I am missing here, but I keep getting unexpected results when using reflect.deepEqual() in my tests. With simple scalar values it works as expected, but I am lost as to why tests that I am expecting to pass are failing and tests that I expect to fail are passing.

Here are the tests in question.

```
func TestNew(t *testing.T) {
    _ = media.SetDefaults("../../../assets/", "../../../assets/Go_gopher_mascot_bw.png")
    type args struct {
        opts []Option
    }
    tests := []struct {
        name string
        args args
        want *Profile
    }{
        {name: "New should return a Profile with the correct gender set", args: args{[]Option{WithGender("male")}}, want: New(WithGender("male"))},
        {name: "New should return a Profile with the correct Avatar set", args: args{[]Option{WithAvatar("../../../assets/picture12371854532127.png")}}, want: New(WithAvatar("../../../assets/picture12371854532127.png"))},
        {name: "New should return a Profile with the correct DOB set", args: args{[]Option{WithDOB(1982, 06, 18)}}, want: New(WithDOB(1982, 06, 18))},
        {name: "New should return a Profile with the correct ScreenName set", args: args{[]Option{WithScreenName("Lars Bak")}}, want: New(WithScreenName("Lars Bak"))},
    }
    for i, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {

            switch i {
            case 1:
                if got := New(tt.args.opts...); !reflect.DeepEqual(got.Gender(), tt.want.gender) {
                    t.Errorf("New() = %v, want %v", got, tt.want)
                }
            case 2:
                if got := New(tt.args.opts...); !reflect.DeepEqual(got.avatar, tt.want.Avatar()) {
                    t.Errorf("New() = %v, want %v", got, tt.want)
                }
            case 3:
                if got := New(tt.args.opts...); !reflect.DeepEqual(got.DOB(), tt.want.dob) {
                    t.Errorf("New() = %v, want %v", got, tt.want)
                }
            case 4:
                if got := New(tt.args.opts...); !reflect.DeepEqual(got.screeName, tt.want.screeName) {
                    t.Errorf("New() = %v, want %v", got.screeName, tt.want.screeName)
                }
            }
        })

    }
}
```

What I am not understanding is why the 4th test is failing , and why is it that if I change my other test so that if they don't have equal values for the fields set, those tests still pass?

Here are the test results.

```
=== RUN   TestNew
--- FAIL: TestNew (0.00s)
=== RUN   TestNew/New_should_return_a_Profile_with_the_correct_gender_set
    --- PASS: TestNew/New_should_return_a_Profile_with_the_correct_gender_set (0.00s)
=== RUN   TestNew/New_should_return_a_Profile_with_the_correct_Avatar_set
    --- PASS: TestNew/New_should_return_a_Profile_with_the_correct_Avatar_set (0.00s)
=== RUN   TestNew/New_should_return_a_Profile_with_the_correct_DOB_set
    --- PASS: TestNew/New_should_return_a_Profile_with_the_correct_DOB_set (0.00s)
=== RUN   TestNew/New_should_return_a_Profile_with_the_correct_ScreenName_set
    --- FAIL: TestNew/New_should_return_a_Profile_with_the_correct_ScreenName_set (0.00s)
        profile_test.go:57: New() = &{[194 90 188 29 133 134 77 43 153 32 125 223 208 91 163 84] Lars Bak {1997 9 13} 0 West-Kshlerin ../../../assets/Go_gopher_mascot_bw.png Medhurst-Flatley 0xc000221e80 {SENEGAL   [882 Meadowshire]  Hauckberg South Carolina 13885 } []}, want &{[219 147 211 8 80 39 74 19 172 10 138 10 2 50 228 153] Lars Bak {1989 9 16} 0 Olson, Nolan and Abbott ../../../assets/Go_gopher_mascot_bw.png Feeney and Sons 0xc000221d00 {EGYPT   [84658 Wellstad]  Raulburgh Montana 42634 } []}
FAIL
```

I am baffled by why the entire struct seems to be being compared as well as printed out by the t.Errorf function. Hoping to gain some clarity about what I am doing wrong here.

Thanks.

  • 写回答

1条回答 默认 最新

  • doujia2386 2019-01-11 04:27
    关注

    i will be zero based so in your 4th test you will be going into the 3rd statement in the switch and comparing the date of birth which you can see from the output is different. The third case statement also prints out the full structure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式