Search code examples
pact

Verification mismatches for Consumer Test


Currently getting this error when running the Pact tests for the consumer side:

Pact verification failed. See output for details. Output: Verification mismatches: [{"method": "GET", "path": "/todos", "request": {"headers": {"Accept": "application/json"}, "method": "GET", "path": "/todos"}, "type": "missing-request"}] Mock server logs:

This is how I've constructed my Consumer Contract Test:

public class TodoControllerTests
{
    private readonly IPactBuilderV3 pactBuilder;
    private readonly List<object> todos;
    
    public TodoContractTests(ITestOutputHelper output)
    {
        Todos = new List<object>()
        {
            New {
                Id = 1,
                Description = "Create Consumer App",
                isDone = false,
                createdDate = "2022-05-02T00:00:00"
            }
            New {
                Id = 2,
                Description = "Create Provider App",
                isDone = false,
                createdDate = "2022-03-21T00:00:00"
            }
        };
        
        var config = new PactConfig
        {
            PactDir = "../../../pacts/"
            Outputters = new []
            {
                New XUnitOutput(output)
            },
            DefaultJsonSettings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            },
        };

        IPactV3 pact = Pact.V3("Something API Consumer", "Something API", config);
        
        this.pactBuilder = pact.UsingNativeBackend();
    }
    
    [Fact]
    public async void Should_get_all_todos()
    {
        this.pactBuilder
            .UponReceiving("A GET request to retrieve todos")
                .Given("There is a list of todos")
                .WithRequest(HttpMethod.Get, "/todos")
                .WithHeader("Accept", "application/json")
            .WillRespond()
                .WithStatus(HttpStatusCode.OK)
                .WithHeader("Content-Type", "application/json; charset=utf-8")
                .WithJsonBody(new TypeMatcher(todos));
        
        await this.pactBuilder.VerifyAsync(async ctx => 
        {
            var client = new HttpClient
            {
                BaseAddress = new Uri("http://localhost:55907")
            };
            
            var todoApiProxy = new TodoApiProxy(client);
            
            var response = await todoApiProxy.GetAll();
        });
    }
}

I've verified that the server is returning the same response as what I've defined in that todos object, so not sure what I'm missing in the verification schema.


Solution

  • The issue is that your HttpClient is sending requests to http://localhost:55907, and not the actual Pact Mock service.

    The error is clear - it's not receiving the request.

    The ctx passed in to VerifyAsync has the address of a dynamic mock server where requests need to be sent.

    Your code could be modified something like:

    var client = new HttpClient
    {
      BaseAddress = new Uri(ctx.MockServerUri)
    };
    ...