Пример #1
0
        public async Task EditAsyncGenericShouldReturnFalseWithInvalidPartnerIdUsingMoq()
        {
            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 EditPartnerViewModel {
            };

            Assert.False(await service.EditAsync <EditPartnerViewModel>(model, null));

            repository.Verify(x => x.AllAsNoTracking(), Times.Once);
        }
Пример #2
0
        public async Task EditAsyncGenericShouldWorkCorrectlyWithLogoUsingMoq()
        {
            AutoMapperConfig.RegisterMappings(typeof(ErrorViewModel).GetTypeInfo().Assembly);

            var repository = new Mock <IDeletableEntityRepository <Partner> >();

            var imageService = new Mock <IImagesService>();

            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());
            repository.Setup(r => r.Update(It.IsAny <Partner>())).Callback((Partner item) =>
            {
                var foundPartner         = partners.FirstOrDefault(x => x.Id == item.Id);
                foundPartner.CompanyName = item.CompanyName;
                foundPartner.CompanyUrl  = item.CompanyUrl;
                foundPartner.LogoUrl     = item.LogoUrl;
            });
            repository.Setup(r => r.SaveChangesAsync()).Verifiable();

            imageService.Setup(r => r.UploadCloudinaryImageAsync(It.IsAny <IFormFile>(), It.IsAny <string>()))
            .Returns(async(IFormFile image, string path) => await Task.FromResult(path + "/" + image.FileName));

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

            var model = new EditPartnerViewModel
            {
                Id          = 1,
                CompanyName = "TestCompanyEdited",
                CompanyUrl  = "TestUrlEdited",
            };

            Assert.True(await service.EditAsync <EditPartnerViewModel>(model, new FormFile(null, 0, 0, "test", "test.png")));
            Assert.Equal("TestCompanyEdited", partners.FirstOrDefault(x => x.Id == 1).CompanyName);
            Assert.Equal("TestUrlEdited", partners.FirstOrDefault(x => x.Id == 1).CompanyUrl);
            Assert.Equal("partners/test.png", partners.FirstOrDefault(x => x.Id == 1).LogoUrl);

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

            imageService.Verify(x => x.UploadCloudinaryImageAsync(It.IsAny <IFormFile>(), It.IsAny <string>()), Times.Once);
        }