public void Task_Edit_Author_Return_View() { Assert.Throws <NotImplementedException>(() => { //Arrange var controller = new AuthorController(context); int id = 1; var auth = new Author() { AuthorId = 1, AuthorName = "saraswati", AuthorDescription = "description", AuthorImage = "image", }; //Act var EditData = controller.Edit(id, auth); //Assert Assert.IsType <ViewResult>(EditData); }); }
public void Edit_POST_ReturnsRedirectToActionResultIfModelStateIsValid() { // FakeAuthorRepository and FakeTempData classes - not used // arrange /* * var rep = new FakeAuthorRepository(); * var controller = new AuthorController(rep) * { * TempData = new FakeTempData() * }; */ // Moq // arrange var rep = new Mock <IRepository <Author> >(); var temp = new Mock <ITempDataDictionary>(); var controller = new AuthorController(rep.Object) { TempData = temp.Object }; // act var result = controller.Edit(new Author()); // assert Assert.IsType <RedirectToActionResult>(result); }
public void EditView_NotNull() { var mock = new Mock <IAuthorLogic>(); mock.Setup(a => a.Edit(new Author())); AuthorController controller = new AuthorController(mock.Object); ViewResult result = controller.Edit(1) as ViewResult; Assert.IsNotNull(result); }
public void EditPostAction_ModelError() { var mock = new Mock <IAuthorLogic>(); AuthorViewModel author = new AuthorViewModel(); var controller = new AuthorController(mock.Object); controller.ModelState.AddModelError("Name", "Model have no name"); ViewResult result = controller.Edit(author) as ViewResult; Assert.IsNotNull(result); }
public void CanEditAuthor(int size) { var mock = CreateAndSetupMock(size); AuthorController target = new AuthorController(mock.Object); for (int i = 0; i < size; ++i) { Author result = GetViewModel <Author>(target.Edit(i + 1)); Assert.Equal(i + 1, result.Id); } }
public void Edit_POST_ReturnsViewResultIfModelStateIsNotValid() { // arrange var rep = new Mock <IRepository <Author> >(); var controller = new AuthorController(rep.Object); controller.ModelState.AddModelError("", "Test error message."); // act var result = controller.Edit(new Author()); // assert Assert.IsType <ViewResult>(result); }
public void Edit_GET_ModelIsAuthorObject() { // arrange var rep = new Mock <IRepository <Author> >(); rep.Setup(m => m.Get(It.IsAny <int>())).Returns(new Author()); var controller = new AuthorController(rep.Object); // act var model = controller.Edit(1).ViewData.Model as Author; // assert Assert.IsType <Author>(model); }
public void Edit_GET_InvalidId_ReturnsHttpNotFound() { //Arrange IRepository mockRepo = Substitute.For <IRepository>(); int invalidid = 999; mockRepo.Authors.Returns(_authors); //Act AuthorController target = new AuthorController(mockRepo); ActionResult result = target.Edit(invalidid); //Assert Assert.IsInstanceOf <NotFoundResult>(result); }
public void CannotSaveInvalidChanges() { Mock <IAuthorRepository> mock = new Mock <IAuthorRepository>(); AuthorController target = new AuthorController(mock.Object); Author author = new Author { Name = "Имя1" }; target.ModelState.AddModelError("error", "error"); IActionResult result = target.Edit(author); mock.Verify(elm => elm.SaveAuthor(It.IsAny <Author>()), Times.Never); Assert.IsType <ViewResult>(result); }
public void Edit_POST_ValidModelButInValidId_ReturnsHttpNotFound() { //Arrange IRepository mockRepo = Substitute.For <IRepository>(); mockRepo.Authors.Returns(_authors); int invaliId = 100; Author validModel = new Author { AuthorId = invaliId, FirstName = "FirstName", LastName = "LastName" }; //Act AuthorController target = new AuthorController(mockRepo); ActionResult result = target.Edit(validModel); //Assert Assert.IsInstanceOf <NotFoundResult>(result); }
public void Edit_GET_ValidId_ReturnsCorrectView() { //Arrange IRepository mockRepo = Substitute.For <IRepository>(); int validid = 1; mockRepo.Authors.Returns(_authors); Author expected = _authors.Where(a => a.AuthorId == validid).First(); //Act AuthorController target = new AuthorController(mockRepo); ActionResult result = target.Edit(validid); //Assert Assert.IsInstanceOf <ViewResult>(result); Author model = (result as ViewResult).Model as Author; Assert.AreEqual(expected, model); }
public void CannotEditNonexistentAuthor(int size) { var mock = CreateAndSetupMock(size); AuthorController target = new AuthorController(mock.Object); int temp = new Random().Next(101); for (int i = 0; i < temp; ++i) { int index = new Random().Next(100); if (i % 2 == 0 || index <= size) { index *= -1; } Author result = GetViewModel <Author>(target.Edit(index)); Assert.Null(result); } }
public void Edit_POST_InvalidModel_ReturnsCorrectView() { //Arrange IRepository mockRepo = Substitute.For <IRepository>(); mockRepo.Authors.Returns(_authors); //Act Author invalidModel = new Author(); AuthorController target = new AuthorController(mockRepo); target.ModelState.AddModelError("error", "some error"); ActionResult result = target.Edit(invalidModel); //Assert Assert.IsInstanceOf <ViewResult>(result); Author model = (result as ViewResult).Model as Author; Assert.AreEqual(invalidModel, model); }
private void EditAuthorBtn_Click(object sender, RoutedEventArgs e) { AuthorViewModel authorVM = dataGrid.SelectedItem as AuthorViewModel; AddAuthor editAuthor = new AddAuthor(authorVM.Name, authorVM.SurName); if (editAuthor.ShowDialog().Value) { string name = editAuthor.Name.Text; string surname = editAuthor.Surname.Text; if (authorVM != null && name != null && surname != null) { Author author = authorController.GetById(authorVM.Id); author.Name = authorVM.Name = name; author.SurName = authorVM.SurName = surname; authorController.Edit(author); } } }
public void CanSaveValidChanges() { Mock <IAuthorRepository> mock = new Mock <IAuthorRepository>(); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); AuthorController target = new AuthorController(mock.Object) { TempData = tempData.Object }; Author author = new Author { Name = "Имя1" }; IActionResult result = target.Edit(author); mock.Verify(elm => elm.SaveAuthor(author)); Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Index", (result as RedirectToActionResult).ActionName); }
public void Edit_POST_ValidModelWithValidId_ReturnsCorrectView() { //Arrange IRepository mockRepo = Substitute.For <IRepository>(); mockRepo.Authors.Returns(_authors); int valiId = 1; Author validModel = new Author { AuthorId = valiId, FirstName = "FirstName", LastName = "LastName" }; Author repoModel = _authors.Where(a => a.AuthorId == valiId).First(); //Act AuthorController target = new AuthorController(mockRepo); ActionResult result = target.Edit(validModel); //Assert mockRepo.Received(1).SaveChanges(); Assert.AreEqual(validModel.FirstName, repoModel.FirstName); Assert.AreEqual(validModel.LastName, repoModel.LastName); Assert.IsInstanceOf <RedirectToActionResult>(result); Assert.AreEqual("Index", (result as RedirectToActionResult).ActionName); }