/// <summary> /// Returns a mock of a DbContext /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static MockedDbContext <T> GetMockContext <T>() where T : DbContext { var instance = new MockedDbContext <T>(); instance.MockTables(); return(instance); }
public void UserRepository_Should_Set_A_Context() { var context = new MockedDbContext(); var repo = new UserRepository(); repo.setContext(context); Assert.AreSame(context, repo.Context); }
public void UserService_Should_Initialize_A_Object() { var mockedRepository = new Mock <IUserRepository>(); var mockedUnitOfWork = new Mock <IUnitOfWork>(); var mockedDbContext = new MockedDbContext(); var service = new UserService(mockedRepository.Object, mockedUnitOfWork.Object, mockedDbContext); Assert.IsInstanceOf <UserService>(service); }
public void RelationshipService_Should_Set_Parameters() { var mockedRepository = new Mock <IRelationshipRepository>(); var mockedUnitOfWork = new Mock <IUnitOfWork>(); var mockedDbContext = new MockedDbContext(); var service = new RelationshipService(mockedRepository.Object, mockedUnitOfWork.Object, mockedDbContext); Assert.IsInstanceOf <RelationshipService>(service); }
public void NotebookService_Repo_Should_Call_SetContext() { var mockedRepository = new Mock <INotebooksRepository>(); mockedRepository.Setup(r => r.setNotebookDbContext(It.IsAny <INotebookDbContext>())); var mockedUnitOfWork = new Mock <IUnitOfWork>(); var mockedDbContext = new MockedDbContext(); var service = new NotebookService(mockedRepository.Object, mockedUnitOfWork.Object, mockedDbContext); mockedRepository.Verify(r => r.setNotebookDbContext(It.IsAny <INotebookDbContext>()), Times.Once); }
public void UserService_Initialization_Should_Call_Repo_SetDbContext() { var mockedRepository = new Mock <IUserRepository>(); mockedRepository.SetupAllProperties(); mockedRepository.Setup(r => r.setContext(It.IsAny <NotebooksDbContext>())); var mockedUnitOfWork = new Mock <IUnitOfWork>(); var mockedDbContext = new MockedDbContext(); var service = new UserService(mockedRepository.Object, mockedUnitOfWork.Object, mockedDbContext); mockedRepository.Verify(r => r.setContext(It.IsAny <NotebooksDbContext>()), Times.Once); }
public void RelationshipService_Should_Call_RepoAdd_When_Call_ServiceAdd() { var mockedRepository = new Mock <IRelationshipRepository>(); mockedRepository.Setup(r => r.Add(It.IsAny <Relationship>())); var mockedUnitOfWork = new Mock <IUnitOfWork>(); var mockedDbContext = new MockedDbContext(); var service = new RelationshipService(mockedRepository.Object, mockedUnitOfWork.Object, mockedDbContext); service.Add(new Relationship()); mockedRepository.Verify(r => r.Add(It.IsAny <Relationship>()), Times.Once); }
/// <summary> /// Mocks all the DbSet{T} properties that represent tables in a DbContext. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mockedContext"></param> public static void MockTables <T>(this MockedDbContext <T> mockedContext) where T : DbContext { Type contextType = typeof(T); var dbSetProperties = contextType.GetProperties().Where(prop => (prop.PropertyType.IsGenericType) && prop.PropertyType.GetGenericTypeDefinition() == typeof(DbSet <>)); foreach (var prop in dbSetProperties) { var dbSetGenericType = prop.PropertyType.GetGenericArguments()[0]; Type listType = typeof(List <>).MakeGenericType(dbSetGenericType); var listForFakeTable = Activator.CreateInstance(listType); var parameter = Expression.Parameter(contextType); var body = Expression.PropertyOrField(parameter, prop.Name); var lambdaExpression = Expression.Lambda <Func <T, object> >(body, parameter); var method = typeof(EntityFrameworkMockHelper).GetMethod("MockDbSet").MakeGenericMethod(dbSetGenericType); mockedContext.Setup(lambdaExpression).Returns(method.Invoke(null, new[] { listForFakeTable })); mockedContext.Tables.Add(prop.Name, listForFakeTable); } }
public void RelationshipService_Should_Call_RepoFind1Teacher_When_Call_ServiceFind1Teacher() { var mockedRepository = new Mock <IRelationshipRepository>(); mockedRepository.Setup(r => r.FindByTeacher(It.IsAny <string>())); var mockedUnitOfWork = new Mock <IUnitOfWork>(); mockedUnitOfWork.Setup(u => u.Commit()); var mockedDbContext = new MockedDbContext(); var service = new RelationshipService(mockedRepository.Object, mockedUnitOfWork.Object, mockedDbContext); service.FindByTeacher("Test"); mockedRepository.Verify(r => r.FindByTeacher(It.IsAny <string>()), Times.Once); }
public void RelationshipService_Should_Call_UOWCommit_When_Call_ServiceDelete() { var mockedRepository = new Mock <IRelationshipRepository>(); mockedRepository.Setup(r => r.Delete(It.IsAny <Relationship>())); var mockedUnitOfWork = new Mock <IUnitOfWork>(); mockedUnitOfWork.Setup(u => u.Commit()); var mockedDbContext = new MockedDbContext(); var service = new RelationshipService(mockedRepository.Object, mockedUnitOfWork.Object, mockedDbContext); service.Delete(new Relationship()); mockedUnitOfWork.Verify(u => u.Commit(), Times.Once); }
public void UserService_CallFind_Should_Call_Repo_Find() { var mockedRepository = new Mock <IUserRepository>(); mockedRepository.SetupAllProperties(); mockedRepository.Setup(r => r.setContext(It.IsAny <NotebooksDbContext>())); mockedRepository.Setup(r => r.Find(It.IsAny <string>())); var mockedUnitOfWork = new Mock <IUnitOfWork>(); mockedUnitOfWork.SetupAllProperties(); mockedUnitOfWork.Setup(u => u.setContext(It.IsAny <NotebooksDbContext>())); var mockedDbContext = new MockedDbContext(); var service = new UserService(mockedRepository.Object, mockedUnitOfWork.Object, mockedDbContext); service.Find("test"); mockedRepository.Verify(r => r.Find(It.IsAny <string>()), Times.Once); }
public void UserService_CallAddClaimToUser_Should_Return_True() { var mockedRepository = new Mock <IUserRepository>(); mockedRepository.SetupAllProperties(); mockedRepository.Setup(r => r.setContext(It.IsAny <NotebooksDbContext>())); mockedRepository.Setup(r => r.Find(It.IsAny <string>())); mockedRepository.Setup(r => r.AddClaimToUser(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())); var mockedUnitOfWork = new Mock <IUnitOfWork>(); mockedUnitOfWork.SetupAllProperties(); mockedUnitOfWork.Setup(u => u.setContext(It.IsAny <NotebooksDbContext>())); mockedUnitOfWork.Setup(u => u.Commit()); var mockedDbContext = new MockedDbContext(); var service = new UserService(mockedRepository.Object, mockedUnitOfWork.Object, mockedDbContext); Assert.IsTrue(service.AddClaimToUser("test", "test", "test")); }
public void UserService_CallContainsClaim_Should_ReturnCorrect_AddClaimToUser(bool value) { var mockedRepository = new Mock <IUserRepository>(); mockedRepository.SetupAllProperties(); mockedRepository.Setup(r => r.setContext(It.IsAny <NotebooksDbContext>())); mockedRepository.Setup(r => r.Find(It.IsAny <string>())); mockedRepository.Setup(r => r.AddClaimToUser(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())); mockedRepository.Setup(r => r.ContainsClaim(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(value); var mockedUnitOfWork = new Mock <IUnitOfWork>(); mockedUnitOfWork.SetupAllProperties(); mockedUnitOfWork.Setup(u => u.setContext(It.IsAny <NotebooksDbContext>())); mockedUnitOfWork.Setup(u => u.Commit()); var mockedDbContext = new MockedDbContext(); var service = new UserService(mockedRepository.Object, mockedUnitOfWork.Object, mockedDbContext); Assert.AreEqual(value, service.ContainsClaim("test", "test", "test")); }
public void UserService_CallAddRoleToUser_Should_Call_UnitOfWOrk_Commit() { var mockedRepository = new Mock <IUserRepository>(); mockedRepository.SetupAllProperties(); mockedRepository.Setup(r => r.setContext(It.IsAny <NotebooksDbContext>())); mockedRepository.Setup(r => r.Find(It.IsAny <string>())); mockedRepository.Setup(r => r.AddClaimToUser(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())); var mockedUnitOfWork = new Mock <IUnitOfWork>(); mockedUnitOfWork.SetupAllProperties(); mockedUnitOfWork.Setup(u => u.setContext(It.IsAny <NotebooksDbContext>())); mockedUnitOfWork.Setup(u => u.Commit()); var mockedDbContext = new MockedDbContext(); var service = new UserService(mockedRepository.Object, mockedUnitOfWork.Object, mockedDbContext); service.AddRoleToUser("test", "test"); mockedUnitOfWork.Verify(u => u.Commit(), Times.Once); }
protected override void RegisterContext(ContainerBuilder builder) { builder.Register(c => MockedDbContext.Create()).As <AirportDbContext>().InstancePerLifetimeScope(); }
protected override void RegisterContext(ContainerBuilder builder) { builder.RegisterInstance(MockedDbContext.Create()).As <SIPlatformDbContext>().InstancePerLifetimeScope(); }