public async Task GetSomething_TheTesterSomethingExists_ReturnsTheSomething()
        {
            // arrange
            const string expectedId        = "83F9262F-28F1-4703-AB1A-8CFD9E8249C9";
            const string expectedFirstName = "some-first-name-03";
            const string expectedLastName  = "some-last-name";
            const int    expectedAge       = 42;

            var expectedSomething = new Something(expectedId, expectedFirstName, expectedLastName, expectedAge);

            const string guidRegex = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}";

            _mockProviderService
            .Given("Some application state")
            .UponReceiving("A GET request to retrieve something")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = Match.Regex($"/somethings/{expectedId}", $"^\\/somethings\\/{guidRegex}$"),
                Query   = "field1=value1&field2=value2",
                Headers = new Dictionary <string, object>
                {
                    { "Accept", "application/json" }
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = Match.Type(new
                {
                    id        = "83F9262F-28F1-4703-AB1A-8CFD9E8249C9",
                    firstName = "some-first-name-03",
                    lastName  = "some-last-name",
                    age       = 42
                })
            });

            var consumer = new SomethingApiClient($"http://localhost:{MockServerPort}");

            // act
            Something actualSomething = await consumer.GetSomething(expectedId).ConfigureAwait(false);

            // assert
            _mockProviderService.VerifyInteractions();
            actualSomething.Should().BeEquivalentTo(expectedSomething);
        }
        public async Task GetSomething_WhenTheTesterSomethingExists_ReturnsTheSomething()
        {
            string expectedId = "tester";

            // Arrange
            _mockProviderService
            .Given("There is a something with id 'tester'")
            .UponReceiving("A Get request to retrieve the Id of something")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = $"/Somethings/{expectedId}",
                Headers = new Dictionary <string, object>
                {
                    { "Accept", "applciation/json" }
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new
                {
                    Id        = "tester",
                    FirstName = "Totally",
                    LastName  = "Awesome"
                }
            });
            var consumer = new SomethingApiClient(_serviceUri);
            // var httpClient = new HttpClient();
            // var response = await httpClient.GetAsync($"{_serviceUri}/somethings/tester");
            // var json = await response.Content.ReadAsStringAsync();
            // var something = JsonConvert.DeserializeObject<Something>(json);

            // Act
            var result = await consumer.GetSomething("tester");

            // Assert
            Assert.Equal(expectedId, result.Id);
            Assert.Equal("Totally", result.FirstName);
            Assert.Equal("Awesome", result.LastName);

            _mockProviderService.VerifyInteractions();
        }
        public void GetSomething_WhenTheTesterSomethingExists_ReturnsTheSomething()
        {
            //Arrange
            _mockProviderService
            .Given("There is a something with id 'tester'")
            .UponReceiving("A GET request to retrieve the something")
            .With(GetTesterSomethingRequest())
            .WillRespondWith(GetTesterSomethingResponse());

            var consumer = new SomethingApiClient(_mockProviderServiceBaseUri);

            //Act
            var result = consumer.GetSomething("tester");

            //Assert
            Assert.Equal("tester", result.id);
            _mockProviderService.VerifyInteractions();
        }
예제 #4
0
    public void GetSomething_WhenTheTesterSomethingExists_ReturnsTheSomething()
    {
        //Arrange
        _mockProviderService
        .Given("There is a something with id 'tester'")
        .UponReceiving("A GET request to retrieve the something")
        .With(new ProviderServiceRequest
        {
            Method  = HttpVerb.Get,
            Path    = "/somethings/tester",
            Headers = new Dictionary <string, object>
            {
                { "Accept", "application/json" }
            }
        })
        .WillRespondWith(new ProviderServiceResponse
        {
            Status  = 200,
            Headers = new Dictionary <string, object>
            {
                { "Content-Type", "application/json; charset=utf-8" }
            },
            Body = new //NOTE: Note the case sensitivity here, the body will be serialised as per the casing defined
            {
                id        = "tester",
                firstName = "Totally",
                lastName  = "Awesome"
            }
        }); //NOTE: WillRespondWith call must come last as it will register the interaction

        var consumer = new SomethingApiClient(_mockProviderServiceBaseUri);

        //Act
        var result = consumer.GetSomething("tester");

        //Assert
        Assert.Equal("tester", result.id);

        _mockProviderService.VerifyInteractions(); //NOTE: Verifies that interactions registered on the mock provider are called at least once
    }