public async Task ShouldCreateMessagePactAfterVerifyingConsumer() { var handler = new RecipeAddedHandler(); var fakePactBrokerMessageHandler = new FakePactBrokerMessageHandler(); fakePactBrokerMessageHandler.Configure(HttpMethod.Put, "http://localhost:9292").RespondsWith(HttpStatusCode.Created); var publisher = new PactPublisher(new HttpClient(fakePactBrokerMessageHandler) { BaseAddress = new Uri("http://localhost:9292") }, "1.0", "local"); var builder = new MessagePactBuilder("messageConsumer", "messageProvider", publisher); await builder.SetUp(_messageBuilder .VerifyConsumer<RecipeAdded>(m => handler.Handle(m))) .BuildAsync(); // Check if handler has been called Assert.IsNotNull(handler.ReceivedRecipes.FirstOrDefault()); Assert.AreEqual("A Recipe", handler.ReceivedRecipes.First().Name); // Check if pact has been published and tagged Assert.AreEqual("messageConsumer", JsonConvert.DeserializeObject<Contract>(fakePactBrokerMessageHandler .GetStatus(HttpMethod.Put, "http://localhost:9292").SentRequestContents.First().Value).Consumer .Name); Assert.IsTrue(fakePactBrokerMessageHandler.GetStatus(HttpMethod.Put, "http://localhost:9292").SentRequestContents.Last().Key.Contains("local")); // Check if pact has been written to project directory. var buildDirectory = AppContext.BaseDirectory; var pactDir = Path.GetFullPath( $"{buildDirectory}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}pacts{Path.DirectorySeparatorChar}"); var pactFile = File.ReadAllText(pactDir + "messageConsumer-messageProvider.json"); Assert.AreEqual("messageConsumer", JsonConvert.DeserializeObject<Contract>(pactFile).Consumer.Name); File.Delete(pactDir + "messageConsumer-messageProvider.json"); }
public async Task ShouldCreateMessagePactAfterVerifyingConsumerAsynchronously() { var handler = new RecipeAddedHandler(); var fakePactBrokerMessageHandler = new FakePactBrokerMessageHandler(); fakePactBrokerMessageHandler.Configure(HttpMethod.Put, "http://localhost:9292").RespondsWith(HttpStatusCode.Created); var publisher = new PactPublisher(new HttpClient(fakePactBrokerMessageHandler) { BaseAddress = new Uri("http://localhost:9292") }, "1.0", "local"); var builder = new MessagePactBuilder("messageConsumer", "messageProvider", publisher); var messsageBuilder = await _messageBuilder .VerifyConsumerAsync<RecipeAdded>(m => handler.HandleAsync(m)); await builder.SetUp(messsageBuilder) .BuildAsync(); // Check if handler has been called Assert.IsNotNull(handler.ReceivedRecipes.FirstOrDefault()); Assert.AreEqual("A Recipe", handler.ReceivedRecipes.First().Name); }