使用beego框架的空主体请求导致API端点测试用例的发布方法失败

时间:2019-07-18 11:00:36

标签: go testing woocommerce-rest-api beego

对于测试端点API返回空响应的Post方法, 我没有到哪里我缺少后方法,我能够测试看,得到api的。这是测试用例代码

func TestCreateFileShare(t *testing.T) {
    var jsonStr = []byte(`{
        "name":"fake_FileShare",
        "description":"fake Fileshare",
        "size":1
    }`)
    var expectedJson = []byte(`{
        "id": "bd5b12a8-a101-11e7-941e-d77981b584d8",
        "name": "fake_FileShare",
        "description": "fake FileShare",
        "size": 1,
        "status": "available",
        "poolId": "084bf71e-a102-11e7-88a8-e31fe6d52248",
        "profileId": "1106b972-66ef-11e7-b172-db03f3689c9c"
    }`)
    var prf = []byte(`{
            "id": "1106b972-66ef-11e7-b172-db03f3689c9c",
            "name":             "default",
            "description"":      "default policy",
            "storageType"":      "file"
    }`)
    var expected model.FileShareSpec
    json.Unmarshal(expectedJson, &expected)

    var res_prf model.ProfileSpec
    json.Unmarshal(prf, &res_prf)
    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("GetDefaultProfileFileShare", c.NewAdminContext()).Return(&res_prf, nil)
        mockClient.On("CreateFileShare", c.NewAdminContext(), &fileshare).Return(&expected, nil)
        db.C = mockClient

        r, _ := http.NewRequest("POST", "/v1beta/file/shares/bd5b12a8-a101-11e7-941e-d77981b584d8", bytes.NewBuffer(jsonStr))
        w := httptest.NewRecorder()
        beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
            httpCtx.Input.SetData("context", c.NewAdminContext())
        })
        beego.BeeApp.Handlers.ServeHTTP(w, r)
        beego.Debug(w)
        var output model.FileShareSpec
        json.Unmarshal(w.Body.Bytes(), &output)
        assertTestResult(t, &output, &expected)
    })
}

输出有404错误消息

2019/07/18 16:16:43.774 [D]  &{404 map[] 404 false <nil> map[] true} 
    --- FAIL: TestCreateFileShare/Should_return_200_if_everything_works_well (0.00s)
        fileshare_test.go:273: expected &{0x134fbe0   sample-fileshare-01 [] This is first sample fileshare for testing 1 default available a5965ebe-dg2c-434t-b28e-f373746a71ca b3585ebe-c42c-120g-b28e-f373746a71ca b7602e18-771e-11e7-8f38-dbd6d291f4eg [192.168.100.100] map[]}, got &{<nil>    []  0      [] map[]}

0 个答案:

没有答案