public async Task CreateArticle_WithSucceedingCommit_ReturnsStatusCodeNoContent()
        {
            var articleForCreationDto = new ArticleForCreationDto()
            {
                Code              = "New Test Article",
                Name1             = "1st article Updated",
                SupplierId        = "supId",
                SupplierReference = "supRef",
                PurchasePrice     = 99.99M,
                Unit              = "ST"
            };
            var newArticle          = _writeMapper.Map <Article>(articleForCreationDto);
            var articleForReturnDto = _writeMapper.Map <ArticleForReturnDto>(newArticle);

            _createCommandMock.Setup(m => m.Execute(It.IsAny <ArticleForCreationDto>())).Returns(Task.FromResult(articleForReturnDto));

            // act
            var result = await _articlesController.CreateArticle(articleForCreationDto);

            // assert
            _createCommandMock.Verify(m => m.Execute(It.IsAny <ArticleForCreationDto>()), Times.Once);
            Assert.That(result, Is.Not.Null);
            Assert.That(result.GetType(), Is.EqualTo(typeof(CreatedAtRouteResult)));
            Assert.That(((CreatedAtRouteResult)result).StatusCode, Is.EqualTo(201));
            Assert.That(((CreatedAtRouteResult)result).RouteName, Is.EqualTo("GetArticle"));
            Assert.That(((CreatedAtRouteResult)result).RouteValues.First(), Is.EqualTo(new KeyValuePair <string, int>("id", articleForReturnDto.Id)));
            Assert.That(((CreatedAtRouteResult)result).Value, Is.EqualTo(articleForReturnDto));
        }
Exemple #2
0
        public void CallCreateArticleService_WhenModelStateIsValid()
        {
            // Arrange
            var articleServiceMock = new Mock <IArticleService>();
            var mappingServiceMock = new Mock <IMappingService>();
            var storeServiceMock   = new Mock <IStoreService>();
            var userServiceMock    = new Mock <IUserService>();
            var utilsMock          = new Mock <IUtilitiesService>();

            var model      = new CreateEditArticleViewModel();
            var controller = new ArticlesController(articleServiceMock.Object, mappingServiceMock.Object, storeServiceMock.Object, userServiceMock.Object, utilsMock.Object);

            var controllerContext = new Mock <ControllerContext>();
            var user = new Mock <IPrincipal>();

            user.Setup(p => p.IsInRole("admin")).Returns(true);
            user.SetupGet(x => x.Identity.Name).Returns("username");
            controllerContext.SetupGet(x => x.HttpContext.User).Returns(user.Object);
            controller.ControllerContext = controllerContext.Object;

            controller.ViewData.ModelState.Clear();

            // Act
            controller.CreateArticle(model);

            // Assert
            articleServiceMock.Verify(x => x.CreateArticle(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once);
        }
Exemple #3
0
        public async Task CreateArticle_GivenValidData_ShouldReturnOK()
        {
            var subject = "Article Subject";
            var body    = "Article Body";

            _mediator.Send(Arg.Any <CreateArticleCommand>()).Returns(true);

            var controller = new ArticlesController(_mediator);

            var request = new CreateArticleDto {
                Subject = subject, Body = body
            };

            IActionResult result = await controller.CreateArticle(request);

            Assert.IsAssignableFrom <IActionResult>(result);
            Assert.IsType <OkResult>(result);
        }