Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何使用`func(w http.ResponseWriter,r*http.Request)模拟测试`_Unit Testing_Http_Go_Mocking_Httphandler - Fatal编程技术网

Unit testing 如何使用`func(w http.ResponseWriter,r*http.Request)模拟测试`

Unit testing 如何使用`func(w http.ResponseWriter,r*http.Request)模拟测试`,unit-testing,http,go,mocking,httphandler,Unit Testing,Http,Go,Mocking,Httphandler,当我想从我的嘲笑中得到回应时,我遇到了问题, 目前我已经创建了一些类似这样的模拟: func (m *MockCarService) GetCar(ctx context.Context, store store.Store, IDCar uint) (interface{}, error) { call := m.Called(ctx, store) res := call.Get(0) if res == nil { return nil, call

当我想从我的嘲笑中得到回应时,我遇到了问题, 目前我已经创建了一些类似这样的模拟:

func (m *MockCarService) GetCar(ctx context.Context, store store.Store, IDCar uint) (interface{}, error) {

    call := m.Called(ctx, store)
    res := call.Get(0)
    if res == nil {
        return nil, call.Error(1)
    }
    return res.(*models.Cars), call.Error(1)
}
func TestGetCar(t *testing.T) {

    var store store.Store

    car := &models.Cars{
        ID:          12345,
        BrandID:     1,
        Name:        "Car abc",
        Budget:      2000,
        CostPerMile: 4000,
        KpiReach:    6000,
    }

    mockService := func() *service.MockCarService {
        svc := &service.MockCarService{}
        svc.On("GetCar", context.Background(), car.ID).Return(car, nil)
        return svc
    }

    handlerGet := NewCarHandler(mockService())
    actualResponse := handlerGet.GetCar(store) 

    expected := `{"success":true,"data":[],"errors":[]}` 
    assert.Equal(t, expected+"\n", actualResponse)
}
func (ah *CampaignHandler) GetCampaigns(store store.Store) func(w http.ResponseWriter, r *http.Request) {
    return func(w http.ResponseWriter, r *http.Request) {  .....
然后我创建handler_test.go,如下所示:

func (m *MockCarService) GetCar(ctx context.Context, store store.Store, IDCar uint) (interface{}, error) {

    call := m.Called(ctx, store)
    res := call.Get(0)
    if res == nil {
        return nil, call.Error(1)
    }
    return res.(*models.Cars), call.Error(1)
}
func TestGetCar(t *testing.T) {

    var store store.Store

    car := &models.Cars{
        ID:          12345,
        BrandID:     1,
        Name:        "Car abc",
        Budget:      2000,
        CostPerMile: 4000,
        KpiReach:    6000,
    }

    mockService := func() *service.MockCarService {
        svc := &service.MockCarService{}
        svc.On("GetCar", context.Background(), car.ID).Return(car, nil)
        return svc
    }

    handlerGet := NewCarHandler(mockService())
    actualResponse := handlerGet.GetCar(store) 

    expected := `{"success":true,"data":[],"errors":[]}` 
    assert.Equal(t, expected+"\n", actualResponse)
}
func (ah *CampaignHandler) GetCampaigns(store store.Store) func(w http.ResponseWriter, r *http.Request) {
    return func(w http.ResponseWriter, r *http.Request) {  .....
我得到的是一些错误(http.HandlerFunc)(0x165e020)(不能将func类型作为参数)

我不知道如何修理它。因为我使用的是这样的处理程序:

func (m *MockCarService) GetCar(ctx context.Context, store store.Store, IDCar uint) (interface{}, error) {

    call := m.Called(ctx, store)
    res := call.Get(0)
    if res == nil {
        return nil, call.Error(1)
    }
    return res.(*models.Cars), call.Error(1)
}
func TestGetCar(t *testing.T) {

    var store store.Store

    car := &models.Cars{
        ID:          12345,
        BrandID:     1,
        Name:        "Car abc",
        Budget:      2000,
        CostPerMile: 4000,
        KpiReach:    6000,
    }

    mockService := func() *service.MockCarService {
        svc := &service.MockCarService{}
        svc.On("GetCar", context.Background(), car.ID).Return(car, nil)
        return svc
    }

    handlerGet := NewCarHandler(mockService())
    actualResponse := handlerGet.GetCar(store) 

    expected := `{"success":true,"data":[],"errors":[]}` 
    assert.Equal(t, expected+"\n", actualResponse)
}
func (ah *CampaignHandler) GetCampaigns(store store.Store) func(w http.ResponseWriter, r *http.Request) {
    return func(w http.ResponseWriter, r *http.Request) {  .....

如果您正在对外部服务进行HTTP调用,并且希望对其进行测试并获得模拟响应,那么可以使用httptest

go中的http包附带了httptest,用于测试所有外部http调用依赖项

请在此处找到一个示例:

如果这不是您的用例,最好使用存根,可以在此处找到实现方法:

基本上,这意味着使用接口并拥有自己的结构和存根函数调用,这些调用将返回您想要的响应

如果您想在测试中添加一些语法糖,可以使用:


希望这能有所帮助。

我在引用的代码中没有看到任何一行会产生该错误,并且您引用的更改与测试中的函数不同。您能否澄清/扩展问题以提供更多信息?该错误不是来自您粘贴的代码,因此无法调试。此外,作为对您标题的回应,没有任何理由在Go中模拟HTTP处理程序。另一方面,由于你的问题不完整,也不清楚你是否真的想这么做。