Beispiel #1
0
        public void Add_ShouldCallCategoryServiceCreate()
        {
            // Arrange
            var categoryService = new Mock <ICategoryService>();

            var identityMock  = new Mock <IIdentity>();
            var principalMock = new Mock <IPrincipal>();

            principalMock.Setup(x => x.Identity).Returns(identityMock.Object);
            var httpContext = new Mock <HttpContextBase>();

            httpContext.Setup(x => x.User).Returns(principalMock.Object);
            var contextMock = new Mock <ControllerContext>();

            contextMock.Setup(x => x.HttpContext).Returns(httpContext.Object);

            var controller = new CategoriesController(categoryService.Object)
            {
                ControllerContext = contextMock.Object
            };

            var model = new CategoryAddViewModel
            {
                Title = "title"
            };

            // Act
            var result = controller.Add(model);

            // Assert
            categoryService.Verify(x => x.Create(It.IsAny <string>(), It.Is <string>(y => y == model.Title)));
        }
        public async void Add_ShouldReturnOk_WhenCategoryIsAdded()
        {
            var category       = CreateCategory();
            var categoryAddDto = new CategoryAddDto()
            {
                Name = category.Name
            };
            var categoryResultDto = MapModelToCategoryResultDto(category);

            _mapperMock.Setup(m => m.Map <Category>(It.IsAny <CategoryAddDto>())).Returns(category);
            _categoryServiceMock.Setup(c => c.Add(category)).ReturnsAsync(category);
            _mapperMock.Setup(m => m.Map <CategoryResultDto>(It.IsAny <Category>())).Returns(categoryResultDto);

            var result = await _categoriesController.Add(categoryAddDto);

            Assert.IsType <OkObjectResult>(result);
        }
Beispiel #3
0
        public void AddRedirectsToTheCategoryIndexView()
        {
            var mockCategoriesContext = new MockCategoriesContext();
            var controller            = new CategoriesController(mockCategoriesContext);
            var result = controller.Add();

            Assert.IsType <RedirectToRouteResult>(result);
            Assert.Equal("Index", result.RouteValues["action"]);
            Assert.Equal("Category", result.RouteValues["controller"]);
        }