微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用golang测试和beego框架验证具有预期输出的Http响应

如何解决无法使用golang测试和beego框架验证具有预期输出的Http响应

我无法使响应主体与期望值匹配 这是我尝试过的方法以创建资源的代码

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 举报,一经查实,本站将立刻删除。