コード例 #1
0
        public async Task CheckFailsWhenSourceCurrencyIsInvalid()
        {
            // Arrange
            response.StatusCode        = HttpStatusCode.BadRequest;
            response.Content           = new StringContent(@"{ ""error"": ""Base 'XXX' is not supported.""}");
            requestData.Price          = 1;
            requestData.SourceCurrency = "XXX";
            requestData.TargetCurrency = "USD";

            var httpClient = new HttpClient(handlerMock.Object);

            factoryMock.Setup(_ => _.CreateClient(It.IsAny <string>()))
            .Returns(httpClient).Verifiable();

            // Act
            var controller        = new GetCurrencyConversionDataController(factoryMock.Object);
            var getDataController = await controller.GetPrice(requestData);

            var expectedUri = new Uri("https://api.exchangeratesapi.io/latest?base=XXX&symbols=USD");

            //Assert
            Assert.NotNull(getDataController);
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);

            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Get && req.RequestUri == expectedUri),
                ItExpr.IsAny <CancellationToken>());
        }
コード例 #2
0
        public async Task CheckCorrectDataIsRetrievedSuccessfullyFromAPI()
        {
            // Arrange
            response.StatusCode        = HttpStatusCode.OK;
            response.Content           = new StringContent(@"{ ""rates"": {""USD"" : 1.3710144928 },""base"": ""GBP"",""date"": ""2021 - 03 - 25""}");
            requestData.Price          = 1;
            requestData.SourceCurrency = "GBP";
            requestData.TargetCurrency = "USD";

            var httpClient = new HttpClient(handlerMock.Object);

            factoryMock.Setup(_ => _.CreateClient(It.IsAny <string>()))
            .Returns(httpClient).Verifiable();

            // Act
            var controller        = new GetCurrencyConversionDataController(factoryMock.Object);
            var getDataController = await controller.GetPrice(requestData);

            var expectedUri = new Uri("https://api.exchangeratesapi.io/latest?base=GBP&symbols=USD");

            //Assert
            Assert.NotNull(getDataController);
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Get && req.RequestUri == expectedUri),
                ItExpr.IsAny <CancellationToken>());
        }
コード例 #3
0
        public void CheckThrowsExceptionWhenSourceAndTargetCurrenciesAreEqual()
        {
            // Act
            var ex = Assert.Throws <ArgumentException>(() => GetCurrencyConversionDataController.ValidateInput("GBP", "GBP"));

            //Assert
            Assert.Equal("Source and Target Currencies must be different.", ex.Message);
        }
コード例 #4
0
        public void CheckThrowsExceptionWhenSourceOrTargetIsNull(string source, string target)
        {
            // Act
            var ex = Assert.Throws <ArgumentNullException>(() => GetCurrencyConversionDataController.ValidateInput(source, target));

            //Assert
            Assert.Equal("Value cannot be null. (Parameter 'Input Currencies cannot be Null.')", ex.Message);
        }