Esempio n. 1
0
        public async Task AddAsync_ShouldAddANewCard()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var expectedDbCard = SampleData.CARD_NOT_ON_THE_LIST;
                var initCards      = SampleData.CREDITCARDS;
                if (initCards.Exists(cc => cc.Id == expectedDbCard.Id || cc.Number == expectedDbCard.Number))
                {
                    throw new InvalidOperationException($"Seeding cards already contain the card that is not supposed to be there: {expectedDbCard}");
                }
                await db.AddRangeAsync(initCards);

                await db.SaveChangesAsync();

                IRepository <CreditCard> repository = new DbCreditCardRepository(db);

                var expectedCards = await repository.GetAllAsync();

                expectedCards.Add(expectedDbCard);

                // Act
                await repository.AddAsync(expectedDbCard);

                var result = await repository.GetAllAsync();

                // Assert

                var actualCards = Assert.IsAssignableFrom <List <CreditCard> >(result);
                Assert.Equal(
                    expectedCards.OrderBy(x => x.Id).Select(x => (id: x.Id, number: x.Number, pin: x.Pin, balance: x.Balance, isValid: x.IsValid)),
                    actualCards.OrderBy(x => x.Id).Select(x => (id: x.Id, number: x.Number, pin: x.Pin, balance: x.Balance, isValid: x.IsValid)));
            }
        }
Esempio n. 2
0
        public async Task AddAsync_ShouldNotAddACard()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var expectedCard = SampleData.CREDITCARDS.First();
                await db.AddRangeAsync(SampleData.CREDITCARDS);

                await db.SaveChangesAsync();

                var expectedDbCard = await db.CreditCards.Where(x => x.Number == expectedCard.Number).FirstAsync();

                IRepository <CreditCard> repository = new DbCreditCardRepository(db);
                var expectedCards = await repository.GetAllAsync();

                // Act
                Func <Task> action = async() => await repository.AddAsync(expectedDbCard);

                // Assert
                await Assert.ThrowsAsync <ArgumentException>(action);
            }
        }