问题描述
我无法使响应主体与期望值匹配 这是我尝试过的方法以创建资源的代码
func TestCreateFileShare(t *testing.T) {
var jsonStr = []byte(`{
"id": "bd5b12a8-a101-11e7-941e-d77981b584d8","name":"fakeFileShare","description":"fake Fileshare","size": 1,"profileId": "1106b972-66ef-11e7-b172-db03f3689c9c","availabilityZone": "default","status": "creating","createdAt":"","updatedAt":""
}`)
t.Run("Should return 200 if everything works well",func(t *testing.T) {
fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{}}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&fileshare)
mockClient := new(dbtest.Client)
mockClient.On("CreateFileShare",c.NewAdminContext(),&fileshare).Return(nil,nil)
mockClient.On("GetProfile","1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleFileShareProfiles[0],nil)
db.C = mockClient
r,_ := http.NewRequest("POST","/v1beta/file/shares",bytes.NewBuffer(jsonStr))
fmt.Println(bytes.NewBuffer(jsonStr))
w := httptest.NewRecorder()
r.Header.Set("Content-Type","application/JSON")
beego.InsertFilter("*",beego.BeforeExec,func(httpCtx *context.Context) {
httpCtx.Input.SetData("context",c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w,r)
assertTestResult(t,w.Code,200)
})
}
或者至少我应该能够忽略一些对于验证不重要的字段,例如:createdAt,updatedAt字段
```
mock: Unexpected Method Call
-----------------------------
The closest call I have is:
- CreatedAt: (string) "",- UpdatedAt: (string) ""
+ CreatedAt: (string) (len=19) "2020-08-28T13:26:09",+ UpdatedAt: (string) (len=19) "2020-08-28T13:26:09"
}),```
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)