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.
Aucun commentaire:
Enregistrer un commentaire