Pact consumer testing in Go. Problem with dsl.Match function

WBOY
Release: 2024-02-11 18:15:08
forward
500 people have browsed it

Go 中的 Pact 消费者测试。 dsl.Match 函数的问题

php editor Apple will share with you today a question about Pact consumer testing in Go. When using Pact for consumer testing, we often use the dsl.Match function to match specific fields in requests and responses. However, we may encounter some problems, such as fields not matching correctly. This article will delve into this issue and provide solutions to help you better deal with the challenges of Pact consumer testing.

Question content

I am writing pact consumer test using go. When I define the interaction I need to add the expected response object. The provider service is written in php and this is the response I expect:

return  [
            'status' => 'success',
            'data' => [
                'configuration' => associative array,
                'undeploy_configuration' => associative array,
                'meta_data' => associative array,
                'undeploy_lora_app_key' => string,
            ],
        ];
Copy after login

This is the object I created in go to represent the response I should get:

deviceconfigurationresponse := dsl.like(map[string]interface{}{
        "status": "success",
        "data": dsl.like(map[string]interface{}{
            "configuration": dsl.mapmatcher{
                "config1": dsl.string("value1"),
                "config2": dsl.string("value2"),
            },
            "undeploy_configuration": dsl.mapmatcher{
                "undeploy1": dsl.string("value3"),
                "undeploy2": dsl.string("value4"),
            },
            "meta_data": dsl.mapmatcher{
                "meta1": dsl.string("info1"),
                "meta2": dsl.string("info2"),
            },
            "undeploy_lora_app_key": dsl.string("example_undeploy_lora_app_key"),
        }),
    })
Copy after login

However, when I run the test, I get this error:

--- fail: testgetdeviceconfiguration (1.79s)
panic: match: unhandled type: interface {} [recovered]
        panic: match: unhandled type: interface {}

Copy after login

This is the complete code:

func TestGetDeviceConfiguration(t *testing.T) {
    // Create Pact client
    pact := &dsl.Pact{
        Consumer: "consumer",
        Provider: "provider",
        PactDir:  "./pacts",
    }

    defer pact.Teardown()

    deviceConfigurationResponse := dsl.Like(map[string]interface{}{
        "status": "success",
        "data": dsl.Like(map[string]interface{}{
            "configuration": dsl.MapMatcher{
                "config1": dsl.String("value1"),
                "config2": dsl.String("value2"),
            },
            "undeploy_configuration": dsl.MapMatcher{
                "undeploy1": dsl.String("value3"),
                "undeploy2": dsl.String("value4"),
            },
            "meta_data": dsl.MapMatcher{
                "meta1": dsl.String("info1"),
                "meta2": dsl.String("info2"),
            },
            "undeploy_lora_app_key": dsl.String("example_undeploy_lora_app_key"),
        }),
    })

    // Define the expected interaction with the provisioning-service
    value := "123456789"
    pact.
        AddInteraction().
        Given("Device configuration exists for the given device ID").
        UponReceiving("A request to get device configuration").
        WithRequest(dsl.Request{
            Method:  "GET",
            Path:    dsl.String(fmt.Sprintf("/api/prov/state/%s/configuration", value)),
            Headers: dsl.MapMatcher{"Accept": dsl.String("application/json")},
        }).
        WillRespondWith(dsl.Response{
            Status:  200,
            Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")},
            Body:    dsl.Match(deviceConfigurationResponse),
        })

    // Test the OnSessionEstablished function
    var test = func() error {
        cache := new(CacheMock)
        deviceConfigGetter := new(DeviceConfigGetterMock)

        _, err := GetDeviceConfiguration(value)

        cache.AssertExpectations(t)
        deviceConfigGetter.AssertExpectations(t)

        return err
    }

    // Verify the interaction with the provider
    var err = pact.Verify(test)
    assert.NoError(t, err)
}
Copy after login

Workaround

The method you are using (match) takes a struct annotated with a struct tag (see https://github.com /pact-foundation/pact-go#auto-generate-matchers-from-struct-tags) specifies how structs should be matched. You've manually provided the struct with the correct matcher, so there's no need to wrap it in match at all.

Something like this should work:

        WillRespondWith(dsl.Response{
            Status:  200,
            Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")},
            Body:    deviceConfigurationResponse,
        })
Copy after login

The above is the detailed content of Pact consumer testing in Go. Problem with dsl.Match function. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template