doumaqing6652 2019-07-18 11:00
浏览 57

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

For testing Post method for an endpoint API returning empty response, I am not getting where i am missing for post method, i am able to test put,get api's. Here is the test case code

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)
    })
}

The output has 404 error message

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧