Пример #1
0
        public void Get_InputId_CalledFindMethodOfDBSetWithCorrectId()
        {
            // Arrange
            DbContextOptions opt = new DbContextOptionsBuilder <OrderContext>()
                                   .Options;
            var mockContext = new Mock <OrderContext>(opt);
            var mockDbSet   = new Mock <DbSet <User> >();

            mockContext
            .Setup(context =>
                   context.Set <User>(
                       ))
            .Returns(mockDbSet.Object);

            User expectedUser = new User()
            {
                OrderID = 1
            };

            mockDbSet.Setup(mock => mock.Find(expectedUser.OrderID))
            .Returns(expectedUser);
            var repository = new TestUserRepository(mockContext.Object);

            //Act
            var actualUser = repository.Get(expectedUser.OrderID);

            // Assert
            mockDbSet.Verify(
                dbSet => dbSet.Find(
                    expectedUser.OrderID
                    ), Times.Once());
            Assert.Equal(expectedUser, actualUser);
        }
Пример #2
0
        public void Create_InputUserInstance_CalledAddMethodOfDBSetWithUserInstance()
        {
            // Arrange
            DbContextOptions opt = new DbContextOptionsBuilder <OrderContext>()
                                   .Options;
            var mockContext = new Mock <OrderContext>(opt);
            var mockDbSet   = new Mock <DbSet <User> >();

            mockContext
            .Setup(context =>
                   context.Set <User>(
                       ))
            .Returns(mockDbSet.Object);
            //EFUnitOfWork uow = new EFUnitOfWork(mockContext.Object);
            var repository = new TestUserRepository(mockContext.Object);

            User expectedUser = new Mock <User>().Object;

            //Act
            repository.Create(expectedUser);

            // Assert
            mockDbSet.Verify(
                dbSet => dbSet.Add(
                    expectedUser
                    ), Times.Once());
        }
Пример #3
0
        public void Delete_InputId_CalledFindAndRemoveMethodsOfDBSetWithCorrectArg()
        {
            // Arrange
            DbContextOptions opt = new DbContextOptionsBuilder <OrderContext>()
                                   .Options;
            var mockContext = new Mock <OrderContext>(opt);
            var mockDbSet   = new Mock <DbSet <User> >();

            mockContext
            .Setup(context =>
                   context.Set <User>(
                       ))
            .Returns(mockDbSet.Object);
            //EFUnitOfWork uow = new EFUnitOfWork(mockContext.Object);
            //IStreetRepository repository = uow.Streets;
            var repository = new TestUserRepository(mockContext.Object);

            User expectedUser = new User()
            {
                OrderID = 1
            };

            mockDbSet.Setup(mock => mock.Find(expectedUser.OrderID)).Returns(expectedUser);

            //Act
            repository.Delete(expectedUser.OrderID);

            // Assert
            mockDbSet.Verify(
                dbSet => dbSet.Find(
                    expectedUser.OrderID
                    ), Times.Once());
            mockDbSet.Verify(
                dbSet => dbSet.Remove(
                    expectedUser
                    ), Times.Once());
        }