Esempio n. 1
0
        public async Task CreateAsyncGenericShouldReturnFalseWithExistingOwnerIdUsingMoq()
        {
            AutoMapperConfig.RegisterMappings(typeof(ErrorViewModel).GetTypeInfo().Assembly);

            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);

            var model = new CreatePartnerInputViewModel {
            };

            Assert.False(await service.CreateAsync <CreatePartnerInputViewModel>(model, "TestManagerId"));

            repository.Verify(x => x.AllAsNoTracking(), Times.Once);
        }
Esempio n. 2
0
        public async Task CreateAsyncGenericShouldIncreaseCountUsingMoq()
        {
            AutoMapperConfig.RegisterMappings(typeof(ErrorViewModel).GetTypeInfo().Assembly);

            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 = 1
                },
                new ApplicationUser {
                    Id = "TestManagerId2", Email = "TestEmail2"
                },
            };

            var partners = new List <Partner>
            {
                new Partner {
                    Id = 1, IsApproved = true, 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.AddAsync(It.IsAny <Partner>())).Callback((Partner item) => partners.Add(item));
            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);

            var model = new CreatePartnerInputViewModel {
            };

            Assert.True(await service.CreateAsync <CreatePartnerInputViewModel>(model, "TestManagerId2"));
            Assert.Equal(3, partners.Count);

            repository.Verify(x => x.AllAsNoTracking(), Times.Once);
            repository.Verify(x => x.AddAsync(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);
        }