コード例 #1
0
        public void Get_InputId_CalledFindMethodOfDBSetWithCorrectId()
        {
            // Arrange
            DbContextOptions opt = new DbContextOptionsBuilder <CatalogContext>()
                                   .Options;
            var mockContext = new Mock <CatalogContext>(opt);
            var mockDbSet   = new Mock <DbSet <Product> >();

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

            Product expectedProduct = new Product()
            {
                ProductID = 1
            };

            mockDbSet.Setup(mock => mock.Find(expectedProduct.ProductID))
            .Returns(expectedProduct);
            var repository = new TestProductRepository(mockContext.Object);

            //Act
            var actualProduct = repository.Get(expectedProduct.ProductID);

            // Assert
            mockDbSet.Verify(
                dbSet => dbSet.Find(
                    expectedProduct.ProductID
                    ), Times.Once());
            Assert.Equal(expectedProduct, actualProduct);
        }
コード例 #2
0
        public void Create_InputProductInstance_CalledAddMethodOfDBSetWithProductInstance()
        {
            // Arrange
            DbContextOptions opt = new DbContextOptionsBuilder <CatalogContext>()
                                   .Options;
            var mockContext = new Mock <CatalogContext>(opt);
            var mockDbSet   = new Mock <DbSet <Product> >();

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

            Product expectedProduct = new Mock <Product>().Object;

            //Act
            repository.Create(expectedProduct);

            // Assert
            mockDbSet.Verify(
                dbSet => dbSet.Add(
                    expectedProduct
                    ), Times.Once());
        }
コード例 #3
0
        public void Delete_InputId_CalledFindAndRemoveMethodsOfDBSetWithCorrectArg()
        {
            // Arrange
            DbContextOptions opt = new DbContextOptionsBuilder <CatalogContext>()
                                   .Options;
            var mockContext = new Mock <CatalogContext>(opt);
            var mockDbSet   = new Mock <DbSet <Product> >();

            mockContext
            .Setup(context =>
                   context.Set <Product>(
                       ))
            .Returns(mockDbSet.Object);
            //EFUnitOfWork uow = new EFUnitOfWork(mockContext.Object);
            //IProductRepository repository = uow.Products;
            var repository = new TestProductRepository(mockContext.Object);

            Product expectedProduct = new Product()
            {
                ProductID = 1
            };

            mockDbSet.Setup(mock => mock.Find(expectedProduct.ProductID)).Returns(expectedProduct);

            //Act
            repository.Delete(expectedProduct.ProductID);

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