Heim  >  Artikel  >  Backend-Entwicklung  >  Pakt-Verbrauchertests in Go. Problem mit der dsl.Match-Funktion

Pakt-Verbrauchertests in Go. Problem mit der dsl.Match-Funktion

WBOY
WBOYnach vorne
2024-02-11 18:15:08449Durchsuche

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

PHP-Editor Apple wird Ihnen heute eine Frage zum Pact-Verbrauchertest in Go stellen. Wenn wir Pact für Verbrauchertests verwenden, verwenden wir häufig die Funktion dsl.Match, um bestimmte Felder in Anfragen und Antworten abzugleichen. Es können jedoch einige Probleme auftreten, z. B. wenn Felder nicht richtig übereinstimmen. Dieser Artikel befasst sich mit diesem Problem und bietet Lösungen, die Ihnen helfen, die Herausforderungen von Pact-Verbrauchertests besser zu bewältigen.

Frageninhalt

Ich schreibe einen Pakt-Verbrauchertest mit go. Wenn ich die Interaktion definiere, muss ich das erwartete Antwortobjekt hinzufügen. Der Anbieterdienst ist in PHP geschrieben und dies ist die Antwort, die ich erwarte:

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

Dies ist das Objekt, das ich in go erstellt habe, um die Antwort darzustellen, die ich erhalten sollte:

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

Wenn ich jedoch den Test durchführe, erhalte ich diese Fehlermeldung:

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

Dies ist der vollständige 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)
}

Lösung

Die Methode, die Sie verwenden (match)采用带有结构标记注释的结构(请参阅https://github.com/pact-foundation/pact-go#auto-generate-matchers-from-struct-tags) 指定如何在结构应该匹配。您已经手动提供了具有正确匹配器的结构,因此根本不需要将其包装在 match in.

So etwas sollte funktionieren:

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

Das obige ist der detaillierte Inhalt vonPakt-Verbrauchertests in Go. Problem mit der dsl.Match-Funktion. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:stackoverflow.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen