https://pact.io logo
#pact-go
Title
# pact-go
d

David Kwak

09/26/2022, 6:31 AM
Using
pact-go v2.0.0-beta-14
on macOS. Two questions: 1) Is there something special I have to do to generate a pact file with multiple interactions for a given consumer? I have a consumer test with 3 cases that pass, but the pact file only seems to have the last interaction in the file 2) Is there something special I have to do to generate an interaction entry for a case that returns an error? I have a
404
test case, and the client under test responds to
404
by returning an error, which I check in my test (using the
testify/assert.Error
function). This test passes yet doesn’t generate an interaction in a pact file, even when the test is the only one executed. Any thoughts would be appreciated.
m

Matt (pactflow.io / pact-js / pact-go)

09/26/2022, 7:25 AM
1. No, it should just append. do you have anything setup to clear the pact file prior to writing to it? Do the
debug
level logs indicate any reason as to why? It’s possible if you use the same description + state it will be overwritten 2. No, it should work as per any other test Could you please share your test so we can see?
d

David Kwak

09/26/2022, 10:50 PM
Thanks for the response, it was helpful. re: #1, it was indeed the ‘UponReceiving’ statement that was duplicated that was leading to overwritten interactions, so I was able to address that issue. re: #2, it seems that ‘not returning’ an error upon a
404
response for the client library resulted in the interaction being recorded. Is there another way to write my test to work with the existing client (I don’t own that code) AND work with
pact-go
? I’m a total noob to
golang
so I am quite unfamiliar with what is acceptable or not. a code snippet of the issue
Copy code
t.Run("404test", func(t *testing.T) {
        // mockProvider is instance of consumer.NewV2Pact
		mockProvider.
			AddInteraction().
			Given("resource does not exist")).
			UponReceiving("GET for resource").
			WithRequest(<GET request>).
			WillRespondWith(404)
}

		err = mockProvider.ExecuteTest(t, func(config consumer.MockServerConfig) error {
			var client Client
			client = Client{
				ServiceURL: fmt.Sprintf("<http://%s:%d>", config.Host, config.Port),
				<additional client config>
			}

			_, err := client.GetCall(<params>)
			assert.Error(t, err)

			return err
		})
    assert.Error(t, err)
})
m

Matt (pactflow.io / pact-js / pact-go)

09/27/2022, 1:14 AM
For #2 the
ExecuteTest
function accepts a user defined test. If the test errors (i.e. the test did not work as expected) you should return an error. If the error is expected, you should test for that. I can see you are doing an
assert.Error(…)
. That’s probably correct, albeit you might want to check the type of that error, because it could potentially fail for other reasons and give you a false positive. I would
return nil
in the case there is no issue with your test, and the assertions are correct. Best to talk to somebody who knows this code better though to ensure what is being tested here is correct and valid
d

David Kwak

09/27/2022, 2:38 AM
thanks for the explanation. i’ll go talk with the code owners to see what can be modified. I appreciate the support here.
2 Views