Exemple #1
0
        public async Task UndeleteAsyncShouldReturnFalseWithInvalidPartnerIdUsingMoq()
        {
            var repository = new Mock <IDeletableEntityRepository <Partner> >();

            var manager = new ApplicationUser {
                Email = "TestEmail", Id = "TestManagerId"
            };
            var partners = new List <Partner>
            {
                new Partner {
                    Id = 1, IsApproved = false, Manager = manager, ManagerId = manager.Id, CompanyName = "TestCompany", CompanyUrl = "TestUrl"
                },
                new Partner {
                    Id = 2, IsApproved = true, Manager = manager
                },
            };

            repository.Setup(r => r.AllAsNoTrackingWithDeleted()).Returns(partners.AsQueryable());

            var service = new PartnersService(repository.Object, null, null);

            Assert.False(await service.UndeleteAsync(42));

            repository.Verify(x => x.AllAsNoTrackingWithDeleted(), Times.Once);
        }
Exemple #2
0
        public async Task UndeleteAsyncShouldWorkCorrectlyUsingMoq()
        {
            var repository = new Mock <IDeletableEntityRepository <Partner> >();

            var store       = new Mock <IUserStore <ApplicationUser> >();
            var userManager = new Mock <UserManager <ApplicationUser> >(store.Object, null, null, null, null, null, null, null, null);

            var managers = new List <ApplicationUser>
            {
                new ApplicationUser {
                    Id = "TestManagerId1", Email = "TestEmail1", PartnerId = null
                },
                new ApplicationUser {
                    Id = "TestManagerId2", Email = "TestEmail2"
                },
            };
            var partners = new List <Partner>
            {
                new Partner {
                    Id = 1, IsApproved = false, IsDeleted = true, DeletedOn = DateTime.UtcNow, Manager = managers.FirstOrDefault(), ManagerId = managers.FirstOrDefault().Id, CompanyName = "TestCompany", CompanyUrl = "TestUrl"
                },
                new Partner {
                    Id = 2, IsApproved = true
                },
            };

            repository.Setup(r => r.AllAsNoTrackingWithDeleted()).Returns(partners.AsQueryable());
            repository.Setup(r => r.Undelete(It.IsAny <Partner>())).Callback((Partner item) =>
            {
                var foundPartner       = partners.FirstOrDefault(x => x.Id == item.Id);
                foundPartner.IsDeleted = false;
                foundPartner.DeletedOn = null;
            });
            repository.Setup(r => r.SaveChangesAsync()).Verifiable();

            userManager.Setup(r => r.FindByIdAsync(It.IsAny <string>()))
            .Returns(async(string userId) => await Task.FromResult <ApplicationUser>(managers.FirstOrDefault(x => x.Id == userId)));
            userManager.Setup(r => r.UpdateAsync(It.IsAny <ApplicationUser>()))
            .Returns(async(ApplicationUser user) =>
            {
                var foundManager       = managers.FirstOrDefault(x => x.Id == user.Id);
                foundManager.PartnerId = user.PartnerId;
                return(await Task.FromResult(IdentityResult.Success));
            });

            var service = new PartnersService(repository.Object, userManager.Object, null);

            Assert.True(await service.UndeleteAsync(1));
            Assert.False(partners.FirstOrDefault().IsDeleted);
            Assert.Null(partners.FirstOrDefault().DeletedOn);
            Assert.Equal(1, managers.FirstOrDefault().PartnerId);

            repository.Verify(x => x.AllAsNoTrackingWithDeleted(), Times.Once);
            repository.Verify(x => x.Undelete(It.IsAny <Partner>()), Times.Once);
            repository.Verify(x => x.SaveChangesAsync());

            userManager.Verify(x => x.FindByIdAsync(It.IsAny <string>()), Times.Once);
            userManager.Verify(x => x.UpdateAsync(It.IsAny <ApplicationUser>()), Times.Once);
        }