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 void ShouldBeAbleToGetSerializedMessage()
        {
            string receivedMessage = null;

            var builder = new MessagePactBuilder("messageConsumer", "messageProvider");

            builder.SetUp(_messageBuilder.VerifyConsumer(m => receivedMessage = m));

            Assert.IsNotNull(receivedMessage);
            var deserializedMessage = JsonConvert.DeserializeObject<RecipeAdded>(receivedMessage);
            Assert.AreEqual("A Recipe", deserializedMessage.Recipe.Name);
        }
Exemple #3
0
        public async Task Handle_WhenProductIsCreated_SavesProduct()
        {
            var stubRepo       = Substitute.For <IProductRepository>();
            var consumer       = new ProductService(stubRepo);
            var productCreated = new Product {
                Id = 3, Name = "Polestar 3", Color = "Red"
            };

            var builder = new MessagePactBuilder("MartinsNetMessageConsumer", "MartinsMessageProvider");

            await builder.SetUp(_messageBuilder
                                .VerifyConsumer <ProductCreated>(e => consumer.HandleSaveProductEvent(e)))
            .BuildAsync();

            stubRepo.Received(1).SaveProduct(productCreated);
        }
        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);
        }