public void Execute_InvokesSaveChanges_OnDbContext() { var context = EntityFrameworkMockHelper.GetMockContext <FakeDbContext>(); context.Setup(x => x.SaveChanges()).Returns(1); var unitOfWork = new EntityFrameworkUnitOfWork(() => context.Object); unitOfWork.Execute(); context.Verify(x => x.SaveChanges(), Times.Once); }
public async void SetUp() { mockContext = EntityFrameworkMockHelper.GetMockContext <ApplicationDbContext>(); var objets = new List <Objet> { new Objet { CategorieID = 1, DatePublication = DateTime.Now, ObjetID = 1, Description = "un objet de test", NomObjet = "Objet1", estDisponible = true, UserID = "1" }, new Objet { CategorieID = 1, DatePublication = DateTime.Now, ObjetID = 2, Description = "un objet de test", NomObjet = "Objet2", estDisponible = false, UserID = "1" }, new Objet { CategorieID = 2, DatePublication = DateTime.Now, ObjetID = 3, Description = "un objet de test", NomObjet = "Objet3", estDisponible = true, UserID = "1" }, new Objet { CategorieID = 2, DatePublication = DateTime.Now, ObjetID = 4, Description = "un objet de test", NomObjet = "Objet4", estDisponible = false, UserID = "2" }, new Objet { CategorieID = 2, DatePublication = DateTime.Now, ObjetID = 5, Description = "un objet de test", NomObjet = "Objet5", estDisponible = true, UserID = "2" }, new Objet { CategorieID = 3, DatePublication = DateTime.Now, ObjetID = 6, Description = "un objet de test", NomObjet = "Objet6", estDisponible = false, UserID = "3" } }.AsQueryable(); mockContext.Object.Objets.AddRange(objets); var users = new List <ApplicationUser>(); users.Add(new ApplicationUser() { Id = "1", UserName = "******", Email = "1000001", Adresse = "666", Nom = "1000001", Prenom = "User1", PhoneNumber = "5145145144", Objets = new List <Objet>() { objets.ElementAt(0), objets.ElementAt(1), objets.ElementAt(2) }, Emprunts = new List <Emprunt>() }); users.Add(new ApplicationUser() { Id = "2", UserName = "******", Email = "1000002", Adresse = "666", Nom = "1000002", Prenom = "User2", PhoneNumber = "5145145155", Objets = new List <Objet>() { objets.ElementAt(3), objets.ElementAt(4) }, Emprunts = new List <Emprunt>() }); users.Add(new ApplicationUser() { Id = "3", UserName = "******", Email = "1000003", Adresse = "666", Nom = "1000003", Prenom = "User3", PhoneNumber = "5145145155", Objets = new List <Objet>(), Emprunts = new List <Emprunt>() }); users.Add(new ApplicationUser() { Id = "4", UserName = "******", Email = "1000004", Adresse = "666", Nom = "1000004", Prenom = "User4", PhoneNumber = "5145145155", Objets = new List <Objet>(), Emprunts = new List <Emprunt>() }); users.Add(new ApplicationUser() { Id = "5", UserName = "******", Email = "1000005", Adresse = "666", Nom = "1000005", Prenom = "User5", PhoneNumber = "5145145155", Objets = new List <Objet>() { objets.ElementAt(5) }, Emprunts = new List <Emprunt>() }); var mockSetUsers = DbSetMocking.CreateMockSet <ApplicationUser>(users); mockContext.Setup(c => c.Users).Returns(mockSetUsers.Object); var categories = new List <Categorie> { new Categorie { CategorieID = 1, Nom = "Divers", Objets = new List <Objet>() { objets.ElementAt(0), objets.ElementAt(1) } }, new Categorie { CategorieID = 2, Nom = "Decorations", Objets = new List <Objet>() { objets.ElementAt(2), objets.ElementAt(3), objets.ElementAt(4) } }, new Categorie { CategorieID = 3, Nom = "Sports", Objets = new List <Objet>() { objets.ElementAt(5) } } }.AsQueryable(); mockContext.Object.Categories.AddRange(categories); }
//[Test] public void edits_are_executed_() { Moq.Mock <SubCategoryEdits> _edit; Moq.Mock <SubCategoriesService> _service; /* * public SubCategoriesService(IRepository<SubCategory> subCategoryRepository * , IServiceEntityEdit<SubCategory> Edits * ) */ var context = new ReposContext("ReposContext"); _edit = new Moq.Mock <SubCategoryEdits>(_Context.Object); var scRepos = new EfRepository <SubCategory>(G_mockUnitOfWork.Object); //var _scRepos = new Mock<EfRepository<SubCategory>>(context); //.Object) { CallBase = true }; var sc = new TestSubCategory { Id = 1, SubCategoryName = new DomainEntityTypeString { Value = "Default" } }; var dataCategories = new List <Category> { new TestCategory { CategoryName = new DomainEntityTypeString { Value = "AAA" } }, new TestCategory { CategoryName = new DomainEntityTypeString { Value = "BBB" } }, new TestCategory { CategoryName = new DomainEntityTypeString { Value = "ZZZ" } } }.AsQueryable(); var mockSet = new Mock <DbSet <SubCategory> >() { CallBase = true }; var DB = new TestDbSet <SubCategory>(); // DbSet<SubCategory>(); mockSet.As <IQueryable <SubCategory> >().Setup(m => m.Provider).Returns(dataCategories.Provider); mockSet.As <IQueryable <SubCategory> >().Setup(m => m.Expression).Returns(dataCategories.Expression); mockSet.As <IQueryable <SubCategory> >().Setup(m => m.ElementType).Returns(dataCategories.ElementType); // mockSet.As<IQueryable<SubCategory>>().Setup(m => m.GetEnumerator()).Returns(o => dataCategories.GetEnumerator()); //_Context.Setup(m => m.Set<SubCategory>()).Returns(DB); var MockDbContext = EntityFrameworkMockHelper.GetMockContext <IDbContext>(); MockDbContext.CallBase = true; MockDbContext.Setup(m => m.Set <SubCategory>()).Returns(DB); var _scRepos = new Mock <IRepository <SubCategory> >() { CallBase = true }; // var mockSet = new Mock<DbSet<SubCategory>>() { CallBase = true }; var x_scRepos = new Mock <EfRepository <SubCategory> >(G_mockUnitOfWork.Object) { CallBase = true }; x_scRepos.Setup(s => s.Add(It.IsAny <SubCategory>())); x_scRepos.Setup(s => s.ModifiedEntities).Returns(It.IsAny <EntityRules>()); var scReposx = new EfRepository <SubCategory>(G_mockUnitOfWork.Object); var testRulesFactory = new TestRulesFactory(); _service = new Moq.Mock <SubCategoriesService>(x_scRepos.Object //_mockSubCategoryRepository.Object , _edit.Object, testRulesFactory) { CallBase = true }; var cat = EntitiesUtil.CreateEntity <SubCategory>(); _edit.Setup(s => s.SetEntitiesDefaults(cat)); var Model = new SubCategoryModel { SubCategoryName = new DomainEntityTypeString { Value = "Defaultx", Attributes = null } }; // _service.Object.CreateServiceEntity(); ModelStateDictionary modelState = new ModelStateDictionary(); // _service.Object.Save(); var subCategoryEntity = new TestSubCategory { Id = 1 , SubCategoryName = new DomainEntityTypeString { Value = "Default" } }; _service.Object.Add(subCategoryEntity); _service.Object.Verify(modelState); // _edit.Object.SetEntitiesDefaults(cat); // _edit.Verify(s => s.SetEntitiesDefaults(cat), Times.Once); }