コード例 #1
0
        public async Task ApproveAsyncShouldReturnFalseWithInvalidPartnerIdUsingMoq()
        {
            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.AllAsNoTracking()).Returns(partners.AsQueryable());

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

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

            repository.Verify(x => x.AllAsNoTracking(), Times.Once);
        }
コード例 #2
0
        public async Task ApproveAsyncShouldWorkCorrectlyUsingMoq()
        {
            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 roles = new List <ApplicationRole>
            {
                new ApplicationRole {
                    Id = "1", Name = "Admin"
                },
                new ApplicationRole {
                    Id = "2", Name = "Partner"
                },
            };

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

            repository.Setup(r => r.AllAsNoTracking()).Returns(partners.AsQueryable());
            repository.Setup(r => r.Update(It.IsAny <Partner>())).Callback((Partner item) =>
            {
                var foundPartner        = partners.FirstOrDefault(x => x.Id == item.Id);
                foundPartner.IsApproved = item.IsApproved;
                foundPartner.ApprovedOn = item.ApprovedOn;
            });
            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.AddToRoleAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>()))
            .Returns(async(ApplicationUser user, string roleName) =>
            {
                var foundManager = managers.FirstOrDefault(x => x.Id == user.Id);
                var foundRole    = roles.FirstOrDefault(x => x.Name == roleName);
                foundManager.Roles.Add(new IdentityUserRole <string> {
                    RoleId = foundRole.Id
                });
                return(await Task.FromResult(IdentityResult.Success));
            });

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

            Assert.True(await service.ApproveAsync(1));
            Assert.True(partners.FirstOrDefault(x => x.Id == 1).IsApproved);
            Assert.Single(managers.FirstOrDefault(x => x.Id == "TestManagerId1").Roles);

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

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