コード例 #1
0
        public async Task ReturnCorrectConvertedBalance_When_PassedValidValue()
        {
            //Arrange
            contextOptions = new DbContextOptionsBuilder <ItsAllAboutTheGameDbContext>()
                             .UseInMemoryDatabase(databaseName: "ReturnCorrectConvertedBalance_When_PassedValidValue")
                             .Options;

            dateTimeProvider = new DateTimeProvider();

            user = new User
            {
                Id           = userId,
                Cards        = new List <CreditCard>(),
                Transactions = new List <Transaction>(),
                UserName     = userName,
                CreatedOn    = dateTimeProvider.Now,
                Email        = email,
                FirstName    = firstName,
                LastName     = lastName,
                DateOfBirth  = DateTime.Parse("02.01.1996"),
                Role         = UserRole.None,
            };

            userWallet = new Wallet
            {
                Currency = Currency.GBP,
                Balance  = 2000, // we put 2000 in balance otherwise the method will return an null DTO (business logic)
                User     = user,
            };

            foreignExchangeServiceMock = new Mock <IForeignExchangeService>();

            foreignExchangeDTO = new ForeignExchangeDTO
            {
                Base  = GlobalConstants.BaseCurrency,
                Rates = Enum.GetNames(typeof(Currency)).ToDictionary(name => name, value => 2m)
            };

            var currencies = foreignExchangeServiceMock.Setup(fesm => fesm.GetConvertionRates()).ReturnsAsync(foreignExchangeDTO);

            //Act
            using (var actContext = new ItsAllAboutTheGameDbContext(contextOptions))
            {
                await actContext.Users.AddAsync(user);

                await actContext.Wallets.AddAsync(userWallet);

                await actContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new ItsAllAboutTheGameDbContext(contextOptions))
            {
                var sut = new WalletService(assertContext, foreignExchangeServiceMock.Object, dateTimeProvider);
                var convertedBalance = await sut.ConvertBalance(user);

                var currentWalletBalance = await assertContext.Wallets.Where(w => w.User == user).FirstOrDefaultAsync();

                var expectedAmount = currentWalletBalance.Balance * foreignExchangeDTO.Rates[userWallet.Currency.ToString()];
                Assert.AreEqual(expectedAmount, convertedBalance);
            }
        }