コード例 #1
0
        public void DeleteVoucherShould_ReturnFalseIfInvalidVoucherId()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_DeleteVoucherInvalid")
                          .Options;
            var dbContext = new CarRentalDbContext(options);

            var user = new ApplicationUser()
            {
                Id        = Guid.NewGuid().ToString(),
                Email     = "*****@*****.**",
                FirstName = "Admin",
                LastName  = "LastAdmin"
            };

            dbContext.Users.Add(user);

            var usersServiceMock = new Mock <IUsersService>();

            usersServiceMock.Setup(p => p.GetUserIdByEmail(user.Email)).
            Returns(user.Id);

            var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper);

            var result = vouchersService.DeleteVoucher(-1).GetAwaiter().GetResult();

            Assert.False(result);
        }
コード例 #2
0
        public void DeleteVoucherShould_DeleteValidVoucher()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_CustomVoucher")
                          .Options;
            var dbContext = new CarRentalDbContext(options);

            var user = new ApplicationUser()
            {
                Id        = Guid.NewGuid().ToString(),
                Email     = "*****@*****.**",
                FirstName = "Admin",
                LastName  = "LastAdmin"
            };

            dbContext.Users.Add(user);

            var usersServiceMock = new Mock <IUsersService>();

            usersServiceMock.Setup(p => p.GetUserIdByEmail(user.Email)).
            Returns(user.Id);

            var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper);

            var result = vouchersService.CreateForUser(user.Email);

            Assert.True(dbContext.Vouchers.Any(x => x.ApplicationUserId == user.Id));

            var voucherId = dbContext.Vouchers.FirstOrDefault().Id;

            vouchersService.DeleteVoucher(voucherId);

            Assert.False(dbContext.Vouchers.Any(x => x.ApplicationUserId == user.Id));
        }