Пример #1
0
        public async Task HappyPath()
        {
            GetNearbyPostcodesWithoutAddressesRequest req = new GetNearbyPostcodesWithoutAddressesRequest()
            {
                Postcode = "NG1 5FS"
            };

            GetNearbyPostcodesWithoutAddresses getNearbyPostcodes = new GetNearbyPostcodesWithoutAddresses(_mediator.Object, _postcodeValidator.Object, _logger.Object);
            IActionResult result = await getNearbyPostcodes.Run(req, CancellationToken.None);

            OkObjectResult objectResult = result as OkObjectResult;

            Assert.IsNotNull(objectResult);
            Assert.AreEqual(200, objectResult.StatusCode);

            ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode> deserialisedResponse = objectResult.Value as ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode>;

            Assert.IsNotNull(deserialisedResponse);

            Assert.IsTrue(deserialisedResponse.HasContent);
            Assert.IsTrue(deserialisedResponse.IsSuccessful);
            Assert.AreEqual(0, deserialisedResponse.Errors.Count());
            Assert.AreEqual("NG1 5FS", deserialisedResponse.Content.NearestPostcodes.FirstOrDefault().Postcode);

            _mediator.Verify(x => x.Send(It.IsAny <GetNearbyPostcodesWithoutAddressesRequest>(), It.IsAny <CancellationToken>()));
        }
Пример #2
0
        public async Task InvalidPostcode()
        {
            _postcodeValidator.Setup(x => x.IsPostcodeValidAsync(It.IsAny <string>())).ReturnsAsync(false);

            GetNearbyPostcodesWithoutAddressesRequest req = new GetNearbyPostcodesWithoutAddressesRequest()
            {
                Postcode = "NG1 5FS"
            };

            GetNearbyPostcodesWithoutAddresses getNearbyPostcodes = new GetNearbyPostcodesWithoutAddresses(_mediator.Object, _postcodeValidator.Object, _logger.Object);
            IActionResult result = await getNearbyPostcodes.Run(req, CancellationToken.None);

            ObjectResult objectResult = result as ObjectResult;

            Assert.IsNotNull(objectResult);
            Assert.AreEqual(422, objectResult.StatusCode);

            ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode> deserialisedResponse = objectResult.Value as ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode>;

            Assert.IsNotNull(deserialisedResponse);

            Assert.IsFalse(deserialisedResponse.HasContent);
            Assert.IsFalse(deserialisedResponse.IsSuccessful);
            Assert.AreEqual(1, deserialisedResponse.Errors.Count());
            Assert.AreEqual(AddressServiceErrorCode.InvalidPostcode, deserialisedResponse.Errors[0].ErrorCode);

            _mediator.Verify(x => x.Send(It.IsAny <GetNearbyPostcodesWithoutAddressesRequest>(), It.IsAny <CancellationToken>()), Times.Never);
        }
Пример #3
0
        public async Task ErrorThrown()
        {
            _mediator.Setup(x => x.Send(It.IsAny <GetNearbyPostcodesWithoutAddressesRequest>(), It.IsAny <CancellationToken>())).Throws <Exception>();

            GetNearbyPostcodesWithoutAddressesRequest req = new GetNearbyPostcodesWithoutAddressesRequest()
            {
                Postcode = "NG1 5FS"
            };

            GetNearbyPostcodesWithoutAddresses getNearbyPostcodes = new GetNearbyPostcodesWithoutAddresses(_mediator.Object, _postcodeValidator.Object, _logger.Object);

            IActionResult result = await getNearbyPostcodes.Run(req, CancellationToken.None);

            ObjectResult objectResult = result as ObjectResult;

            Assert.IsNotNull(objectResult);

            ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode> deserialisedResponse = objectResult.Value as ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode>;

            Assert.IsNotNull(deserialisedResponse);
            Assert.AreEqual(500, objectResult.StatusCode);;


            Assert.IsFalse(deserialisedResponse.HasContent);
            Assert.IsFalse(deserialisedResponse.IsSuccessful);
            Assert.AreEqual(1, deserialisedResponse.Errors.Count());
            Assert.AreEqual(AddressServiceErrorCode.UnhandledError, deserialisedResponse.Errors[0].ErrorCode);

            _mediator.Verify(x => x.Send(It.IsAny <GetNearbyPostcodesWithoutAddressesRequest>(), It.IsAny <CancellationToken>()));

            _logger.Verify(x => x.LogErrorAndNotifyNewRelic(It.Is <string>(y => y.Contains("Unhandled error")), It.IsAny <Exception>(), It.IsAny <GetNearbyPostcodesWithoutAddressesRequest>()));
        }