Hey guys, I'm testing message-queue-based service ...
# pact-go
d
Hey guys, I'm testing message-queue-based service (lambda) and have a question: Currently I get the error when I try to validate the contract:
[ERROR] API handler start failed: accept tcp [::]:42053
But basically I just need to validate the Handler's "handle" function and can do it in my unit tests by calling that func with mocked Handler's dependencies. I want to do the same with my contract tests. Is it possible to validate the contract wihout the need to run local Handler or the running Handler is required?
if i'm not mistaken it shouldn't launch any mocks for messages as per description
m
Could you please share your code?
d
@Matt (pactflow.io / pact-js / pact-go) thanks for your answer! My sample code is: https://github.com/denispeganovri/sampleHandler And unfortunately, I'm not able to run your example:
den@WIN-S95P35K3NPM:/.../pact-go/examples$ go test -run TestMessagePact
...
../consumer/http.go:82:31: undefined: native.MockServer
../consumer/http.go:114:24: undefined: native.NewHTTPPact
../consumer/http.go:117:48: undefined: native.SPECIFICATION_VERSION_V2
../consumer/http.go:119:48: undefined: native.SPECIFICATION_VERSION_V3
../consumer/http.go:121:48: undefined: native.SPECIFICATION_VERSION_V4
../consumer/http.go:123:9: undefined: native.Init
../consumer/http.go:316:16: undefined: native.GetTLSConfig
../consumer/interaction.go:16:35: undefined: mockserver.Interaction
../consumer/http_v4.go:461:65: undefined: native.INTERACTION_PART_REQUEST
../consumer/http_v4.go:542:65: undefined: native.INTERACTION_PART_RESPONSE
../consumer/http_v4.go:542:65: too many errors
FAIL    <http://github.com/pact-foundation/pact-go/v2/examples|github.com/pact-foundation/pact-go/v2/examples> [build failed]
And your example seems to be helpful, could you help me run this consumer_v3_test please?
In my code I get such error when I try to validate the contract:
den@WIN-S95P35K3NPM:/.../sampleHandler/handler$ go test -run Test_verifyContract
2023/03/23 15:51:47 [INFO] checking pact-provider-verifier within range >= 1.36.1, < 2.0.0
2023/03/23 15:51:47 [INFO] checking pact-broker within range >= 1.22.3
2023/03/23 15:51:48 [INFO] checking pact-mock-service within range >= 3.5.0, < 4.0.0
2023/03/23 15:51:48 [WARN] state handler not found for state: given a SQS message
2023/03/23 15:51:48 [ERROR] *message handler not found for message description: expected to receive no error*
2023/03/23 15:51:48 [ERROR] *API handler start failed: accept tcp [::]:41565: use of closed network connection*
--- FAIL: Test_verifyContract (0.86s)
--- FAIL: Test_verifyContract/has_matching_content (0.00s)
pact.go:638: Verifying a pact between Sample Consumer and Sample Handler Given given a SQS message expected to receive no error has matching content
pact.go:644: Verifying a pact between Sample Consumer and Sample Handler Given given a SQS message expected to receive no error has matching content
An error was raised while verifying the message. The response body is:
FAIL
exit status 1
FAIL    sampleHandler/handler   0.867s
m
hmm strange. See also https://pact-foundation.slack.com/archives/C9UTHTFFB/p1679884470364199?thread_ts=1679663992.765239&amp;cid=C9UTHTFFB. Let’s stick to one of the threads so we can follow the context if that’s OK?
👍 1
OK looked at your code, there are 3 problems: 1. The scenario in your consumer doesn’t match up with the mapping in your provider 2. The provider state in your consumer doesn’t match up with the mapping in your provider 3. Your consumer is exact value matching, and your provider is returning a different value. Either return the exact value in your provider, or use a matcher in your consumer
Diff:
Copy code
diff --git a/handler/handler_contract_test.go b/handler/handler_contract_test.go
index 11d6b8b..ed1b4b1 100644
--- a/handler/handler_contract_test.go
+++ b/handler/handler_contract_test.go
@@ -3,8 +3,9 @@ package handler
 import (
 	"path/filepath"
 
-	"<http://github.com/pact-foundation/pact-go/dsl|github.com/pact-foundation/pact-go/dsl>"
 	"testing"
+
+	"<http://github.com/pact-foundation/pact-go/dsl|github.com/pact-foundation/pact-go/dsl>"
 )
 
 var data *SQSEvent
@@ -14,7 +15,7 @@ func Test_verifyContract(t *testing.T) {
 
 	// Map test descriptions to message producer (handlers)
 	functionMappings := dsl.MessageHandlers{
-		"received data": func(m dsl.Message) (interface{}, error) {
+		"expected to receive no error": func(m dsl.Message) (interface{}, error) {
 			if data != nil {
 				return data, nil
 			} else {
@@ -26,7 +27,7 @@ func Test_verifyContract(t *testing.T) {
 	}
 
 	stateMappings := dsl.StateHandlers{
-		"data exists": func(s dsl.State) error {
+		"given a SQS message": func(s dsl.State) error {
 			data = &SQSEvent{
 				SqsData: "12341234",
 			}
(sorry about the formatting part of the diff, my editor does that automatically)
The error is a red herring. It should actually just be a warning
d
@Matt (pactflow.io / pact-js / pact-go) Many thanks for your detailed answer! Glad it's not a "real" error and will look into my code.
👍 1
m
Awesome! No worries, thanks for the repro
👍 1