示例#1
0
        public async void TestWithException()
        {
            var quoteProvider = Substitute.For <IQuoteProvider>();

            quoteProvider
            .WhenForAnyArgs(x => x.GetCurrencyValues("", null))
            .Do(x =>
            {
                throw new QuoteException(201, "You have supplied an invalid Source Currency. [Example: source=BRL]");
            });

            var currencySource = "HUEHUE";
            var currencyTarget = "USD";

            var currencyLayerService = new QuoteCurrencyService(quoteProvider);
            var controller           = new QuoteController(currencyLayerService);
            var controllerResult     = await controller.GetQuotes(currencySource, currencyTarget);

            Assert.IsType <ObjectResult>(controllerResult);

            var formattedControllerResult = ((ObjectResult)controllerResult);

            Assert.Equal(500, formattedControllerResult.StatusCode);

            var quoteException = (QuoteException)formattedControllerResult.Value;

            Assert.Equal(201, quoteException.ErrorCode);
            Assert.Equal("You have supplied an invalid Source Currency. [Example: source=BRL]", quoteException.Message);
        }
示例#2
0
        public async Task ParsedResultTestOk()
        {
            var quoteProvider = Substitute.For <IQuoteProvider>();

            var currencySource = "BRL";
            var currencyQuotes = new List <string>()
            {
                "USD", "AUD", "CAD", "PLN", "MXN"
            };

            var fakeResult = new CurrencyLayerResult()
            {
                Success   = true,
                Terms     = "https://currencylayer.com/terms",
                Privacy   = "https://currencylayer.com/privacy",
                Timestamp = 1533403523,
                Source    = currencySource,
                Quotes    = new Dictionary <string, double> {
                    { "BRLUSD", 1.300102 },
                    { "BRLAUD", 1.756633 },
                    { "BRLCAD", 1.691108 },
                    { "BRLPLN", 4.790361 },
                    { "BRLMXN", 24.124743 }
                },
                Error = null
            };

            quoteProvider
            .GetCurrencyValues(currencySource, currencyQuotes)
            .Returns(fakeResult);

            var currencyLayerService = new QuoteCurrencyService(quoteProvider);
            var quoteResult          = await currencyLayerService.GetQuote(currencySource, currencyQuotes);

            Assert.Equal(currencySource, quoteResult.Source);

            foreach (var quoteItem in quoteResult.Quotes)
            {
                var quoteName = $"{currencySource}{quoteItem.CurrencyName}";
                Assert.Equal(fakeResult.Quotes[quoteName], quoteItem.CurrencyValue);
            }
        }
示例#3
0
        public async void TestWithSuccess()
        {
            var quoteProvider = Substitute.For <IQuoteProvider>();

            var currencySource = "BRL";
            var currencyQuotes = new List <string>()
            {
                "USD", "AUD", "CAD", "PLN", "MXN"
            };

            var fakeResult = new CurrencyLayerResult()
            {
                Success   = true,
                Terms     = "https://currencylayer.com/terms",
                Privacy   = "https://currencylayer.com/privacy",
                Timestamp = 1533403523,
                Source    = currencySource,
                Quotes    = new Dictionary <string, double> {
                    { "BRLUSD", 0.269757 }
                },
                Error = null
            };

            quoteProvider
            .GetCurrencyValues(currencySource, currencyQuotes)
            .ReturnsForAnyArgs(fakeResult);

            var currencyLayerService = new QuoteCurrencyService(quoteProvider);
            var controller           = new QuoteController(currencyLayerService);
            var controllerResult     = await controller.GetQuotes(currencySource, "USD");

            Assert.IsType <OkObjectResult>(controllerResult);
            var quoteCurrencyResult = (QuoteCurrencyResult)((OkObjectResult)controllerResult).Value;

            Assert.Equal(currencySource, quoteCurrencyResult.Source);
            Assert.Single(quoteCurrencyResult.Quotes);
            Assert.Equal("USD", quoteCurrencyResult.Quotes[0].CurrencyName);
            Assert.Equal(0.269757, quoteCurrencyResult.Quotes[0].CurrencyValue);
        }