public void CreateTest() { // Act ViewResult result = controller.Create() as ViewResult; // Asserts Assert.IsNotNull(result); var model = result.Model; Assert.IsNotNull(model); Assert.IsInstanceOf <NewsTimeViewModel>(model); Assert.IsNotNull(((NewsTimeViewModel)model).News); }
public void Post_Create_Model_Not_Valid() { // Arrange var newsViewModel = new NewsViewModel() { AddedDate = DateTime.Now, //Position = 1, //Title = "New 1", Content = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa." }; var validator = new ModelValidator <NewsViewModel>(newsViewModel); var service = Substitute.For <INewsRepository>(); var mapper = Substitute.For <IMapper>(); var controller = new NewsController(service, mapper); // Act var valid = validator.IsValid(); validator.AddToModelError(controller); var result = controller.Create(newsViewModel) as RedirectToRouteResult; var tempData = controller.TempData["ModelIsNotValid"]; // Assert Assert.That(valid, Is.False); Assert.That(result, !Is.Null); Assert.That("Wystąpił błąd w formularzu, spróbuj ponownie.", Is.EqualTo(tempData)); }
public async Task Add_Should_ReturnNewsArticle() { var mockRepo = new Mock <IDataRepository <NewsArticle, long> >(MockBehavior.Strict); var mocklogger = new Mock <ILogger <NewsController> >(); var newsArticle = new NewsArticle { Id = 1, AuthorName = "Author", Body = "Test1", DatePublished = DateTime.UtcNow, Title = "Title" }; mockRepo.Setup(repo => repo.Add(newsArticle)).Returns(Task.FromResult(GetNewsArticle())).Verifiable(); var controller = new NewsController(mockRepo.Object, mocklogger.Object); var result = await controller.Create(newsArticle) as CreatedAtRouteResult; Assert.IsNotNull(result); Assert.AreEqual((int)HttpStatusCode.Created, result.StatusCode); mockRepo.Verify(); var returnedValue = (NewsArticle)result.Value; Assert.AreEqual(1, returnedValue.Id); Assert.AreEqual("Title", returnedValue.Title); }
public async void CreatePostAction_SaveModel() { // arrange var mock = new Mock <IRepository <News> >(); var news = new News(); var controller = new NewsController(mock.Object); // act RedirectToRouteResult result = await controller.Create(news) as RedirectToRouteResult; // assert mock.Verify(a => a.AddAsync(news)); }
public async Task Add_Should_ReturnBadRequest() { var mockRepo = new Mock <IDataRepository <NewsArticle, long> >(MockBehavior.Strict); var mocklogger = new Mock <ILogger <NewsController> >(); mockRepo.Setup(repo => repo.Add(null)).Verifiable(); var controller = new NewsController(mockRepo.Object, mocklogger.Object); var result = await controller.Create(null) as BadRequestResult; Assert.IsNotNull(result); Assert.AreEqual((int)HttpStatusCode.BadRequest, result.StatusCode); }
public async void AddNewsReturnsViewResultWithNewsModel() { // Arrange var mock = new Mock <IRepository <News> >(); var controller = new NewsController(mock.Object); controller.ModelState.AddModelError("Title", "Required"); News newNews = new News(); // Act var result = await controller.Create(newNews); // Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.Equal(newNews, viewResult?.Model); }
public void CreateNewItemWithBadDataReturnsBadRequest() { //Arrange var mockNewsService = new Mock <INewsService>(); var sut = new NewsController(mockNewsService.Object); //Act var result = sut.Create(new CreateEditNewsModel() { Title = "", Content = "Blabla", PublishDate = DateTime.Now }); //Assert Assert.IsType <BadRequestObjectResult>(result); }
public async void AddNewsReturnsARedirectAndAddsNews() { // Arrange var mock = new Mock <IRepository <News> >(); var controller = new NewsController(mock.Object); var newNews = new News() { Title = "First title" }; // Act var result = await controller.Create(newNews); // Assert var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Null(redirectToActionResult.ControllerName); Assert.Equal("Index", redirectToActionResult.ActionName); mock.Verify(r => r.AddAsync(newNews)); }
public void ValidModelStateOnGet() { // Assign var news = GetFakeNews(); var dateTimeFactory = new DateTimeFactory(); var fileServiceMock = new Mock <IFileStorageService>(); DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(news), ensureDeleted: true); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = new NewsController(context, dateTimeFactory, fileServiceMock.Object); // Act var response = newsController.Create(); // Assert Assert.IsType <ViewResult>(response); } }
public void CreateItemReturnsOk() { //Arrange TestUtils testUtils = new TestUtils(); var mockService = new Mock <INewsService>(); mockService.Setup( m => m.Create(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DateTime>())) .Returns(() => new NewsInfoModel() { Id = 1, Title = "TestTitle", Content = "Blabla", PublishDate = new DateTime(2017, 12, 12) }); var sut = new NewsController(mockService.Object); var model = new CreateEditNewsModel() { Title = "TestTitle", Content = "Blabla", PublishDate = new DateTime(2017, 12, 12) }; //Act var result = sut.Create(model); //Assert Assert.IsType <OkObjectResult>(result); var resultData = (result as OkObjectResult).Value as NewsInfoModel; Assert.True(resultData.Title == model.Title && resultData.Content == model.Content && resultData.PublishDate == model.PublishDate); }
public void Good_Post_Create() { // Arrange var newsViewModel = new NewsViewModel() { AddedDate = DateTime.Now, Position = 1, Title = "New", Content = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa." }; var news = new News() { AddedDate = DateTime.Now, Position = 1, Title = "New", Content = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa." }; var validator = new ModelValidator <NewsViewModel>(newsViewModel); var service = Substitute.For <INewsRepository>(); var mapper = Substitute.For <IMapper>(); var controller = new NewsController(service, mapper); mapper.Map <NewsViewModel, News>(newsViewModel).Returns(news); service.Insert(news); service.Save(); // Act var valid = validator.IsValid(); validator.AddToModelError(controller); var result = controller.Create(newsViewModel) as RedirectToRouteResult; var actionName = result.RouteValues.Values.ElementAt(0); // Assert Assert.That(result, !Is.Null); Assert.That("Index", Is.EqualTo(actionName)); Assert.That(valid, Is.True); }
public async Task PostCreateShouldReturnRedirectWithValidModel() { // Arrange string titleValue = "TestNews"; string descriptionValue = "DescriptionNews"; string imageUrlValue = "test.com/dadsefaef"; string modelUserId = null; string modelTitle = null; string modelDescription = null; string modelImageUrl = null; string successMessage = null; var userManager = this.GetUserManagerMock(); var newsService = new Mock <INewsService>(); newsService .Setup(c => c.CreateAsync( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Callback((string userId, string title, string description, string imageUrl) => { modelUserId = userId; modelTitle = title; modelDescription = description; modelImageUrl = imageUrl; }) .Returns(Task.CompletedTask); var tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[TempDataSuccessMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => successMessage = message as string); var controller = new NewsController(newsService.Object, userManager.Object); controller.TempData = tempData.Object; // Act var result = await controller.Create(new NewsCreateViewModel { Title = titleValue, Description = descriptionValue, ImageUrl = imageUrlValue }); // Assert Assert.Equal(titleValue, modelTitle); Assert.Equal(descriptionValue, modelDescription); Assert.Equal(imageUrlValue, modelImageUrl); Assert.Equal("News created successfully!", successMessage); Assert.IsType <RedirectToActionResult>(result); Assert.Equal("All", (result as RedirectToActionResult).ActionName); Assert.Equal(null, (result as RedirectToActionResult).ControllerName); }