0

我无法将响应正文与预期值匹配这是我尝试使用 POST 方法创建资源的代码

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", c.NewAdminContext(),"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"
  }),
```
4

0 回答 0