我有以下代码
func TestBookingListing_provisionHistory(t *testing.T) {
Setup(false)
b := Booking{
Status: StatusActive,
ListingIDs: []int64{1, 2},
}
for _, l := range b.ListingIDs {
bl := BookingListing{
BookingID: b.ID,
ListingID: l,
Created: b.Created,
Status: StatusPending,
RequestedBy: "Jane",
Type: b.Type,
Updated: b.Created,
}
if Status(bl.RequestedBy) != StatusExpired {
t.Error("expecting status of bookinglisting to be requested")
}
}
}
当状态未过期时,我需要在中显示Requestedby的名称。如何做到这一点提前感谢
如果我正确理解你,你只需要这样做:
// other code here...
if Status(bl.RequestedBy) != StatusExpired {
t.Error("expecting status of bookinglisting to be requested")
return
}
fmt.Printf("requested by: %sn", bl.RequestedBy)
// ...
或者如果状态过期,测试不应该返回:
// other code here...
if Status(bl.RequestedBy) != StatusExpired {
t.Error("expecting status of bookinglisting to be requested")
} else {
fmt.Printf("requested by: %sn", bl.RequestedBy)
}
// ...
您想要的是t.Errorf而不是t.Error,然后您可以插入值:
if Status(bl.RequestedBy) != StatusExpired {
t.Errorf("incorrect bookinglisting status got:%d want:%d",bl.RequestedBy, StatusExpired)
}