示例#1
0
        public async void Test2()
        {
            var options = new DbContextOptionsBuilder <SMarketContext>()
                          .UseInMemoryDatabase(databaseName: "TestDBMarket")
                          .Options;

            using (var context = new SMarketContext(options))
            {
                ProductType model = new ProductType();
                model.Name        = "Gaseosas ";
                model.Description = " Bebidas con gas  ";

                var           service = new ProductTypesController(context);
                IActionResult post    = await service.PostProductType(model);
            }

            using (var context = new SMarketContext(options))
            {
                List <ProductType> listModel = await context.ProductType.ToListAsync();

                int count = listModel.Count;
                Console.WriteLine("ValorCounte:" + count);
                Assert.Equal(2, count);
                //Assert.AreEqual("http://sample.com", context.Blogs.Single().Url);
            }

            // Use a separate instance of the context to verify correct data was saved to database
        }
示例#2
0
        public void DeleteTest()
        {
            var db          = new DmQT03Entities();
            var productType = new ProductType
            {
                ProductTypeName = "ProductName",
                ProductTypeCode = "123",
            };

            var controller = new ProductTypesController();
            var context    = new Mock <HttpContextBase>();
            var session    = new Mock <HttpSessionStateBase>();

            session.Setup(s => s["UserName"]).Returns("abc");
            context.Setup(c => c.Session).Returns(session.Object);
            controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);

            using (var scope = new TransactionScope())
            {
                db.ProductTypes.Add(productType);
                db.SaveChanges();
                var count   = db.ProductTypes.Count();
                var result2 = controller.DeleteConfirmed(productType.ID) as RedirectToRouteResult;
                Assert.IsNotNull(result2);
                Assert.AreEqual(count - 1, db.ProductTypes.Count());
            }
        }
示例#3
0
        public async Task ExceptionTest()
        {
            // Arrange
            IProductTypeRepository mockRepo = Substitute.For <IProductTypeRepository>();

            mockRepo
            .GetAllProductTypesAsync()
            .ThrowsForAnyArgs(new Exception("Test Exception"));

            IDistributedCache   mockCache   = Substitute.For <IDistributedCache>();
            IOptions <Settings> appSettings = Substitute.For <IOptions <Settings> >();

            var mockLocalizer = new MockStringLocalizer <ProductTypesController>();

            IProductTypesApi theApi = new ProductTypesApi(appSettings, mockRepo);
            var controller          = new ProductTypesController(mockCache, theApi, mockLocalizer);

            //// Act
            var actionResult = await controller.Get();

            var objectResult = actionResult as Microsoft.AspNetCore.Mvc.ObjectResult;

            ////// Assert
            Assert.NotNull(objectResult);
            Assert.Equal(objectResult.StatusCode, (int)System.Net.HttpStatusCode.InternalServerError);
        }
示例#4
0
        public void CreateGetTest()
        {
            var controller = new ProductTypesController();
            var context    = new Mock <HttpContextBase>();
            var session    = new Mock <HttpSessionStateBase>();

            context.Setup(c => c.Session).Returns(session.Object);
            controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
            session.Setup(s => s["UserName"]).Returns("abc");

            var result = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
        }
        public async Task DeleteProductType_ValidRequest_SuccessResult()
        {
            var id = Guid.NewGuid();

            //Arrange
            BaseMediator.Setup(x => x.Send(It.IsAny <DeleteProductTypeCommand>(), new CancellationToken())).
            ReturnsAsync(true);
            var productTypesController = new ProductTypesController(BaseMediator.Object);

            //Action
            var result = await productTypesController.Delete(id);

            //Assert
            Assert.True(result.Value);
        }
示例#6
0
        public void EditGetTest()
        {
            var db         = new DmQT03Entities();
            var controller = new ProductTypesController();
            var context    = new Mock <HttpContextBase>();
            var session    = new Mock <HttpSessionStateBase>();

            context.Setup(c => c.Session).Returns(session.Object);
            controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
            session.Setup(s => s["UserName"]).Returns("abc");

            var productTypesID = db.ProductTypes.First().ID;

            var result = controller.Edit(productTypesID) as ViewResult;

            Assert.IsNotNull(result);
        }
示例#7
0
        public void CreatePostTest()
        {
            var controller = new ProductTypesController();
            var db         = new DmQT03Entities();
            var context    = new Mock <HttpContextBase>();
            var request    = new Mock <HttpRequestBase>();
            var files      = new Mock <HttpFileCollectionBase>();
            var file       = new Mock <HttpPostedFileBase>();

            using (var scope = new TransactionScope())
            {
                var model = new ProductType();
                model.ProductTypeName = "ProductTypeName";
                model.ProductTypeCode = "TES";
                var result0 = controller.Create(model) as RedirectToRouteResult;
                Assert.IsNotNull(result0);
            }
        }
        public async Task CreateProductType_ValidRequest_SuccessResult()
        {
            var requestModel = new CreateProductTypeCommand
            {
                ProductTypeKey  = "ProductKey",
                ProductTypeName = "ProductTypeName"
            };


            //Arrange
            BaseMediator.Setup(x => x.Send(requestModel, new CancellationToken())).
            ReturnsAsync(true);
            var productTypesController = new ProductTypesController(BaseMediator.Object);

            //Action
            var result = await productTypesController.Post(requestModel);

            //Assert
            Assert.True(result.Value);
        }
示例#9
0
        public void EditPostTest()
        {
            var controller = new ProductTypesController();
            var db         = new DmQT03Entities();
            var context    = new Mock <HttpContextBase>();
            var request    = new Mock <HttpRequestBase>();
            var files      = new Mock <HttpFileCollectionBase>();
            var file       = new Mock <HttpPostedFileBase>();

            var model = db.ProductTypes.AsNoTracking().First();

            using (var scope = new TransactionScope())
            {
                model.ProductTypeCode = "TES";
                model.ProductTypeName = "TEST";

                var result = controller.Edit(model) as RedirectToRouteResult;

                Assert.IsNotNull(result);
            }
        }
示例#10
0
        public void IndexTest()
        {
            var controller = new ProductTypesController();
            var context    = new Mock <HttpContextBase>();
            var session    = new Mock <HttpSessionStateBase>();

            context.Setup(c => c.Session).Returns(session.Object);
            controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
            session.Setup(s => s["UserName"]).Returns("abc");

            var result = controller.Index() as ViewResult;
            var db     = new DmQT03Entities();

            Assert.IsInstanceOfType(result.Model, typeof(List <ProductType>));
            Assert.AreEqual(db.ProductTypes.Count(), (result.Model as List <ProductType>).Count);

            session.Setup(s => s["UserName"]).Returns(null);
            var redirect = controller.Index() as RedirectToRouteResult;

            Assert.AreEqual("Login", redirect.RouteValues["action"]);
            Assert.AreEqual("Login", redirect.RouteValues["controller"]);
        }
        public async Task GetProductTypeDetailsById_ValidRequest_SuccessResult()
        {
            var productTypeId = Guid.NewGuid();
            var response      = new ProductTypeDetailsResponseModel
            {
                ProductTypeID   = productTypeId,
                ProductTypeKey  = "ProductKey",
                ProductTypeName = "ProductTypeName"
            };


            //Arrange
            BaseMediator.Setup(x => x.Send(It.IsAny <GetProductTypeDetailsQuery>(), new CancellationToken())).
            ReturnsAsync(response);
            var productTypesController = new ProductTypesController(BaseMediator.Object);

            //Action
            var result = await productTypesController.Get(productTypeId);

            //Assert
            Assert.IsType <ActionResult <ProductTypeDetailsResponseModel> >(result);
        }
        public async Task GetProductTypes_ValidRequest_SuccessResult()
        {
            var response = new List <ProductTypeResponseModel>();

            response.Add(new ProductTypeResponseModel
            {
                ProductTypeID   = Guid.NewGuid(),
                ProductTypeKey  = "ProductKey",
                ProductTypeName = "ProductTypeName"
            });

            //Arrange
            BaseMediator.Setup(x => x.Send(It.IsAny <FetchProductTypeQuery>(), new CancellationToken())).
            ReturnsAsync(response);
            var productTypesController = new ProductTypesController(BaseMediator.Object);

            //Action
            var result = await productTypesController.Get();

            //Assert
            Assert.IsType <List <ProductTypeResponseModel> >(result);
        }
示例#13
0
        public async Task SuccessTest()
        {
            // Arrange
            IProductTypeRepository mockRepo = Substitute.For <IProductTypeRepository>();

            var repositoryReturnValue = new List <ProductTypeModel>()
            {
                new ProductTypeModel()
                {
                    Description   = "Description 1",
                    ProductTypeId = 1
                },
                new ProductTypeModel()
                {
                    Description   = "Description 2",
                    ProductTypeId = 2
                }
            };

            mockRepo
            .GetAllProductTypesAsync()
            .ReturnsForAnyArgs(Task.FromResult <IEnumerable <ProductTypeModel> >(repositoryReturnValue));

            IDistributedCache   mockCache   = Substitute.For <IDistributedCache>();
            IOptions <Settings> appSettings = Substitute.For <IOptions <Settings> >();

            var mockLocalizer = new MockStringLocalizer <ProductTypesController>();

            IProductTypesApi theApi = new ProductTypesApi(appSettings, mockRepo);
            var controller          = new ProductTypesController(mockCache, theApi, mockLocalizer);

            //// Act
            var response = await controller.Get();

            ////// Assert
            var actualRecord = ((Microsoft.AspNetCore.Mvc.ObjectResult)response).Value;

            Assert.Equal(((List <ProductTypeModel>)actualRecord).Count, repositoryReturnValue.Count);
        }
 public ProductTypesControllerTest()
 {
     context = new Mock <IApplicationDbContext>();
     context.Setup(x => x.ProductTypes).Returns(() => productTypes.ToMockDbSet());
     controller = new ProductTypesController(context.Object);
 }
 public ProductTypesApiTest()
 {
     _seed       = Guid.NewGuid().ToString();
     _controller = MockController.CreateApi <ProductTypesController>(new DataContext(_seed, DBTypeEnum.Memory), "user");
 }