对于每个字段,我都具有以下带有getter的结构
```
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
}
```
每个字段的类型只是字符串的包装,这些字符串会进行一些验证或其他操作。吸气剂的格式相同,
```
// 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
}
```
构造函数在下面,在没有提供用于设置struct字段的选项的情况下,为其提供了随机默认值。
```
type Option func(prof *Profile)
func New(opts ...Option) *Profile {
p := newWithAllDefaults()
for _, opt := range opts {
opt(p)
}
return p
}
```
对所有可以传递给构造函数的Option类型进行测试,并在Profile结构上设置未导出的字段。
现在我遇到的问题是测试。我现在仅使用go大约3个月了,所以我确定这里缺少某些东西,但是在测试中使用reflect.deepEqual()时,我总是得到意想不到的结果。使用简单的标量值,它可以按预期工作,但是我对为什么要通过的测试失败以及为什么要通过的测试感到困惑。
这是有问题的测试。
```
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)
}
}
})
}
}
```
我不明白的是为什么第四项测试失败了,为什么如果我更改另一项测试以使它们在设置的字段中没有相等的值,这些测试仍然可以通过?
这是测试结果。
```
=== 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
```
我对为什么似乎要比较整个结构以及t.Errorf函数将其打印出来感到困惑。希望能弄清楚我在这里做错了什么。
谢谢。
答案 0 :(得分:0)
foreach ($_POST as $k => $v) {
settype($_POST[$k], gettype($v));
}}
echo json_encode($_POST);
将基于零,因此在您的第4个测试中,您将进入开关中的第3个语句,并比较从输出中可以看到的出生日期。第三个case语句还会打印出完整结构。