完成这个单元测试
This commit is contained in:
parent
51a33052d9
commit
1a0f2be95f
|
@ -13,12 +13,17 @@ func TestCaseAddressList(t *testing.T) {
|
||||||
var resp *requests.Response
|
var resp *requests.Response
|
||||||
var result gjson.Result
|
var result gjson.Result
|
||||||
|
|
||||||
|
// 获取 session,并携带 JWT token
|
||||||
ses := GetSesssionWithJwtToken(t, gserver)
|
ses := GetSesssionWithJwtToken(t, gserver)
|
||||||
|
|
||||||
|
// 向服务器发送 GET 请求,获取用户地址列表
|
||||||
resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/address-list", cnf.Host, cnf.Port)).TestInServer(gserver)
|
resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/address-list", cnf.Host, cnf.Port)).TestInServer(gserver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
// log.Println(resp.ContentString())
|
||||||
|
|
||||||
|
// 检查返回值中的 code 字段是否存在,并且值是否为 200
|
||||||
result = resp.Json().Get("code")
|
result = resp.Json().Get("code")
|
||||||
if !result.Exists() {
|
if !result.Exists() {
|
||||||
t.Error("code is not exists")
|
t.Error("code is not exists")
|
||||||
|
@ -27,6 +32,7 @@ func TestCaseAddressList(t *testing.T) {
|
||||||
t.Error("code != 200")
|
t.Error("code != 200")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查返回值中的 msg 字段是否存在,并且值是否为 "success"
|
||||||
result = resp.Json().Get("msg")
|
result = resp.Json().Get("msg")
|
||||||
if !result.Exists() {
|
if !result.Exists() {
|
||||||
t.Error("msg is not exists")
|
t.Error("msg is not exists")
|
||||||
|
@ -34,4 +40,52 @@ func TestCaseAddressList(t *testing.T) {
|
||||||
if result.String() != "success" {
|
if result.String() != "success" {
|
||||||
t.Error(result.String())
|
t.Error(result.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查返回值中的 data 字段是否存在
|
||||||
|
result = resp.Json().Get("data")
|
||||||
|
if !result.Exists() {
|
||||||
|
t.Error("data is not exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查返回值中的每个地址信息是否符合预期
|
||||||
|
for _, address := range result.Array() {
|
||||||
|
// 检查地址信息中的各个字段是否存在并且不为空
|
||||||
|
if !address.Get("id").Exists() || address.Get("id").Int() == 0 {
|
||||||
|
t.Error("address id is not exists or empty")
|
||||||
|
}
|
||||||
|
if !address.Get("user_id").Exists() || address.Get("user_id").Int() == 0 {
|
||||||
|
t.Error("address user_id is not exists or empty")
|
||||||
|
}
|
||||||
|
if !address.Get("name").Exists() || address.Get("name").String() == "" {
|
||||||
|
t.Error("address name is not exists or empty")
|
||||||
|
}
|
||||||
|
if !address.Get("mobile").Exists() || address.Get("mobile").String() == "" {
|
||||||
|
t.Error("address mobile is not exists or empty")
|
||||||
|
}
|
||||||
|
if !address.Get("street").Exists() || address.Get("street").String() == "" {
|
||||||
|
t.Error("address street is not exists or empty")
|
||||||
|
}
|
||||||
|
if !address.Get("city").Exists() || address.Get("city").String() == "" {
|
||||||
|
t.Error("address city is not exists or empty")
|
||||||
|
}
|
||||||
|
if !address.Get("state").Exists() || address.Get("state").String() == "" {
|
||||||
|
t.Error("address state is not exists or empty")
|
||||||
|
}
|
||||||
|
if !address.Get("country").Exists() || address.Get("country").String() == "" {
|
||||||
|
t.Error("address country is not exists or empty")
|
||||||
|
}
|
||||||
|
if !address.Get("zip_code").Exists() || address.Get("zip_code").String() == "" {
|
||||||
|
t.Error("address zip_code is not exists or empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查地址信息中的 is_default 字段是否存在,并且值是否为布尔类型
|
||||||
|
if !address.Get("is_default").Exists() {
|
||||||
|
t.Error("address is_default is not exists or not a boolean")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查地址信息中的 status 字段是否存在,并且值是否为 1
|
||||||
|
if !address.Get("status").Exists() || address.Get("status").Int() != 1 {
|
||||||
|
t.Error("address status is not exists or != 1")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user