|
1 | 1 | using System; |
| 2 | +using System.IO; |
2 | 3 | using Xunit; |
3 | | -using PactNet.Mocks.MockHttpService; |
4 | | -using PactNet.Mocks.MockHttpService.Models; |
| 4 | +using Xunit.Abstractions; |
| 5 | +using PactNet; |
5 | 6 | using Consumer; |
6 | 7 | using System.Collections.Generic; |
7 | | -using PactNet.Matchers.Type; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Net; |
| 10 | +using PactNet.Matchers; |
8 | 11 | using FluentAssertions; |
| 12 | +using PactNet.Infrastructure.Outputters; |
| 13 | +using PactNet.Output.Xunit; |
| 14 | +using System.Threading.Tasks; |
9 | 15 |
|
10 | 16 | namespace tests |
11 | 17 | { |
12 | | - public class ConsumerPactTests : IClassFixture<ConsumerPactClassFixture> |
| 18 | + public class ConsumerPactTests |
13 | 19 | { |
14 | | - private IMockProviderService _mockProviderService; |
15 | | - private string _mockProviderServiceBaseUri; |
| 20 | + private IPactBuilderV3 pact; |
| 21 | + // private readonly int port = 9222; |
16 | 22 |
|
17 | | - public ConsumerPactTests(ConsumerPactClassFixture fixture) |
| 23 | + private readonly List<object> products; |
| 24 | + |
| 25 | + public ConsumerPactTests(ITestOutputHelper output) |
18 | 26 | { |
19 | | - _mockProviderService = fixture.MockProviderService; |
20 | | - _mockProviderService.ClearInteractions(); //NOTE: Clears any previously registered interactions before the test is run |
21 | | - _mockProviderServiceBaseUri = fixture.MockProviderServiceBaseUri; |
| 27 | + |
| 28 | + products = new List<object>() |
| 29 | + { |
| 30 | + new { id = "27", name = "burger", type = "food" } |
| 31 | + }; |
| 32 | + |
| 33 | + var Config = new PactConfig |
| 34 | + { |
| 35 | + PactDir = Path.Join("..", "..", "..", "..", "pacts"), |
| 36 | + Outputters = new List<IOutput> { new XunitOutput(output), new ConsoleOutput() }, |
| 37 | + LogLevel = PactLogLevel.Debug |
| 38 | + }; |
| 39 | + |
| 40 | + pact = Pact.V3("pactflow-example-consumer-dotnet", "pactflow-example-provider-dotnet", Config).WithHttpInteractions(); |
22 | 41 | } |
23 | 42 |
|
24 | 43 | [Fact] |
25 | | - public async void RetrieveProducts() |
| 44 | + public async Task RetrieveProducts() |
26 | 45 | { |
27 | 46 | // Arrange |
28 | | - _mockProviderService.Given("products exist") |
29 | | - .UponReceiving("A request to get products") |
30 | | - .With(new ProviderServiceRequest |
31 | | - { |
32 | | - Method = HttpVerb.Get, |
33 | | - Path = "/products", |
34 | | - }) |
35 | | - .WillRespondWith(new ProviderServiceResponse { |
36 | | - Status = 200, |
37 | | - Headers = new Dictionary<string, object> |
38 | | - { |
39 | | - { "Content-Type", "application/json; charset=utf-8" } |
40 | | - }, |
41 | | - Body = new MinTypeMatcher(new |
42 | | - { |
43 | | - id = "27", |
44 | | - name = "burger", |
45 | | - type = "food" |
46 | | - }, 1) |
47 | | - }); |
48 | | - |
49 | | - // Act |
50 | | - var consumer = new ProductClient(); |
51 | | - List<Product> result = await consumer.GetProducts(_mockProviderServiceBaseUri); |
52 | | - |
53 | | - // Assert |
54 | | - result.Should().NotBeNull(); |
55 | | - result.Should().HaveCount(1); |
56 | | - result[0].id.Should().Equals("27"); |
57 | | - result[0].name.Should().Equals("burger"); |
58 | | - result[0].type.Should().Equals("food"); |
| 47 | + pact.UponReceiving("A request to get products") |
| 48 | + .Given("products exist") |
| 49 | + .WithRequest(HttpMethod.Get, "/products") |
| 50 | + .WillRespond() |
| 51 | + .WithStatus(HttpStatusCode.OK) |
| 52 | + .WithHeader("Content-Type", "application/json; charset=utf-8") |
| 53 | + .WithJsonBody(Match.MinType(products[0],1)); |
| 54 | + |
| 55 | + await pact.VerifyAsync(async ctx => |
| 56 | + { |
| 57 | + // Act |
| 58 | + var consumer = new ProductClient(); |
| 59 | + List<Product> result = await consumer.GetProducts(ctx.MockServerUri.ToString().TrimEnd('/')); |
| 60 | + // Assert |
| 61 | + result.Should().NotBeNull(); |
| 62 | + result.Should().HaveCount(1); |
| 63 | + Assert.Equal("27",result[0].id); |
| 64 | + Assert.Equal("burger",result[0].name); |
| 65 | + Assert.Equal("food",result[0].type); |
| 66 | + }); |
59 | 67 | } |
60 | 68 | } |
61 | 69 | } |
0 commit comments