コード例 #1
0
        public void Test_SaleController_GetById(int id)
        {
            var fakeContext = new FakeContext("SaleController_GetById");

            fakeContext.FillWithAll();

            using (var context = new MainContext(fakeContext.FakeOptions, fakeContext.FakeConfiguration().Object))
            {
                var repository        = new SaleRepository(context);
                var productRepository = new ProductRepository(context);
                var updateMock        = new Mock <IUpdateProduct>();
                updateMock
                .Setup(x => x.UpdateStock(It.IsAny <Sale>(), It.IsAny <Sale>()));

                var messageMock = new Mock <IMessageHandler>();
                messageMock
                .Setup(x => x
                       .SendMessageAsync(It.IsAny <MessageType>(), It.IsAny <Sale>(), It.IsAny <UpdatedSale>()))
                .Returns(Task.CompletedTask);

                var service     = new SaleService(repository, messageMock.Object, updateMock.Object, productRepository);
                var controller  = new SaleController(service);
                var response    = controller.GetById(id);
                var okResult    = response as OkObjectResult;
                var resultValue = okResult.Value;

                Assert.NotNull(okResult);
                Assert.Equal(200, okResult.StatusCode);
                Assert.Equal(repository.GetById(id), okResult.Value);
                Assert.IsType <Sale>(resultValue);
            }
        }