예제 #1
0
        public void Verify_QueriesMethods_WorksProperly()
        {
            int setCalls = 0;

            var dbsetMock = new Mock <DbSet <TestEntity> >();

            var dbModelMock = new Mock <EfDbModel>();

            dbModelMock.Setup(x => x.Set <TestEntity>()).Returns(dbsetMock.Object);

            Func <string, EfDbModel> createDbContextBehaviour = (cs) =>
            {
                return(dbModelMock.Object);
            };

            var dbContext = new EfDbContext(createDbContextBehaviour);

            dbContext.CreateDbContext();

            var queryable = dbContext.Query <TestEntity>();

            setCalls++;

            dbModelMock.Verify(x => x.Set <TestEntity>(), Times.Exactly(setCalls));

            dbContext.Add(new TestEntity());
            setCalls++;

            dbModelMock.Verify(x => x.Set <TestEntity>(), Times.Exactly(setCalls));
            dbsetMock.Verify(x => x.Add(It.IsAny <TestEntity>()), Times.Once());

            dbContext.AddRange(new List <TestEntity>());
            setCalls++;

            dbModelMock.Verify(x => x.Set <TestEntity>(), Times.Exactly(setCalls));
            dbsetMock.Verify(x => x.AddRange(It.IsAny <List <TestEntity> >()), Times.Once());

            dbContext.Remove(new TestEntity());
            setCalls++;

            dbModelMock.Verify(x => x.Set <TestEntity>(), Times.Exactly(setCalls));
            dbsetMock.Verify(x => x.Remove(It.IsAny <TestEntity>()), Times.Once());

            dbContext.RemoveRange(new List <TestEntity>());
            setCalls++;

            dbModelMock.Verify(x => x.Set <TestEntity>(), Times.Exactly(setCalls));
            dbsetMock.Verify(x => x.RemoveRange(It.IsAny <List <TestEntity> >()), Times.Once());
        }
예제 #2
0
        public void Verify_CreateDbContext_IsCalledCorrectly()
        {
            var dbModelMock = new Mock <EfDbModel>();

            bool funcCalled = false;
            Func <string, EfDbModel> createDbContextBehaviour = (cs) =>
            {
                funcCalled = true;

                return(dbModelMock.Object);
            };

            var dbContext = new EfDbContext(createDbContextBehaviour);

            dbContext.CreateDbContext();
            Assert.True(funcCalled);
        }
예제 #3
0
        public void Verify_SaveChanges_WorksProperly()
        {
            var dbModelMock = new Mock <EfDbModel>();

            dbModelMock.Setup(x => x.SaveChanges()).Returns(0);

            Func <string, EfDbModel> createDbContextBehaviour = (cs) =>
            {
                return(dbModelMock.Object);
            };

            var dbContext = new EfDbContext(createDbContextBehaviour);

            dbContext.CreateDbContext();

            int result = dbContext.SaveChanges();

            dbModelMock.Verify(x => x.SaveChanges(), Times.Once());
        }