public void Create_Always_CallsRepositoryWithData() { // arrange const string TAG_NAME = "tag name"; var incomingData = new TagSaveData { Name = TAG_NAME }; var createdSummary = new TagSummary(); _mockTagRepository.Setup(r => r.Create(It.IsAny <TagSaveData>())).Returns(createdSummary); // act _tagsController.Create(incomingData); // assert _mockTagRepository.Verify(r => r.Create(It.Is <TagSaveData>(sd => sd.Name == TAG_NAME))); }
private async Task CreateTagsAsync() { var c = new TagsController(_context); foreach (var tag in tags) { var result = await c.Create(new TagModel { TagID = tag.TagID, ActivityID = tag.ActivityID }); } }
public async void TagsController_Create() { var ctrlr = new TagsController(_dbContext); Tag tag = new Tag() { TagName = "misc" }; var result = await ctrlr.Create(tag); var resultView = Assert.IsType <RedirectToActionResult>(result); Assert.Equal(1, _dbContext.Tag.Count(d => d.TagName == "misc")); Tag dupTag = new Tag() { TagName = "misc" }; result = await ctrlr.Create(dupTag); Assert.Equal(1, _dbContext.Tag.Count(d => d.TagName == "misc")); //last comment has potential fix http://stackoverflow.com/questions/37558049/modelstate-isvalid-always-true-when-testing-controller-in-asp-net-mvc-web-api //Owner blankOwner = new Owner() //{ // Name = "" //}; //result = await ctrlr.Create(blankOwner); //Assert.Equal(0, _dbContext.Owner.Count(d => d.Name == "")); Tag charTag = new Tag() { TagName = "`1234567890-=~!@#$%^&*()_+{}|:\"<>?[]\\;',./" }; result = await ctrlr.Create(charTag); resultView = Assert.IsType <RedirectToActionResult>(result); Assert.Equal(1, _dbContext.Tag.Count(d => d.TagName == "`1234567890-=~!@#$%^&*()_+{}|:\"<>?[]\\;',./")); }
public async Task CreateTag_ReturnsCreatedResult_WhenTagIsNotInTheSystem() { // Arrange var mockTagRepo = new Mock <ITagService>(); var newUserId = new Guid(); var tag = new Tag { Name = "tag", CreatorId = newUserId.ToString(), CreatedOn = DateTime.Now }; mockTagRepo.Setup(repo => repo.CreateTagAsync(It.IsAny <Tag>())) .ReturnsAsync(true); var autoMapperConfiguration = new MapperConfiguration(cfg => { cfg.CreateMap <Tag, CreateTagRequest>(); cfg.CreateMap <Tag, TagResponse>(); } ); var mapper = autoMapperConfiguration.CreateMapper(); var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim("id", newUserId.ToString()) }, "mock")); var tagController = new TagsController(mapper, mockTagRepo.Object); tagController.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = user } }; // Act var result = await tagController.Create(mapper.Map <CreateTagRequest>(tag)); // Assert var resultCraeted = result.Should().BeCreatedResult(); resultCraeted.Value.Should().BeEquivalentTo(mapper.Map <TagResponse>(tag)); resultCraeted.Location.Should().NotBeEmpty(); }
public async void Create_Errors() { TagsControllerMockFacade mock = new TagsControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiTagsResponseModel> >(new FluentValidation.Results.ValidationResult()); var mockRecord = new ApiTagsResponseModel(); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiTagsRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiTagsResponseModel> >(mockResponse.Object)); TagsController controller = new TagsController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Create(new ApiTagsRequestModel()); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiTagsRequestModel>())); }
public async void Create_No_Errors() { TagsControllerMockFacade mock = new TagsControllerMockFacade(); var mockResponse = new CreateResponse <ApiTagsResponseModel>(new FluentValidation.Results.ValidationResult()); mockResponse.SetRecord(new ApiTagsResponseModel()); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiTagsRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiTagsResponseModel> >(mockResponse)); TagsController controller = new TagsController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Create(new ApiTagsRequestModel()); response.Should().BeOfType <CreatedResult>(); (response as CreatedResult).StatusCode.Should().Be((int)HttpStatusCode.Created); var createResponse = (response as CreatedResult).Value as CreateResponse <ApiTagsResponseModel>; createResponse.Record.Should().NotBeNull(); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiTagsRequestModel>())); }