public void AnswerController_Index_Post_ShouldReturnAnswerWithInstanceOfDateTimeSet() { // Arrange var data = new Mock <IUowData>(); data.Setup(d => d.Answers.All()).Returns(AnswersCollection().AsQueryable()); var claim = new Claim("test", "id-123"); var identity = new Mock <ClaimsIdentity>(); identity.Setup(i => i.FindFirst(It.IsAny <string>())).Returns(claim); var principal = new Mock <IPrincipal>(); principal.Setup(p => p.Identity).Returns(identity.Object); var context = new Mock <ControllerContext>(); context.Setup(c => c.HttpContext.User).Returns(principal.Object); AnswerController controller = new AnswerController(data.Object) { ControllerContext = context.Object }; Answer answer = new Answer(); // Act var result = controller.Index(answer, 1, "ThreadTitle") as ViewResult; // Assert data.Verify(a => a.Answers.Add(It.Is <Answer>(x => x.Published.GetType() == typeof(DateTime)))); }
public void AnswerController_Index_Post_ShouldRedirectWithCorrectParamThreadTitle(string threadTitle) { // Arrange var data = new Mock <IUowData>(); data.Setup(d => d.Answers.All()).Returns(AnswersCollection().AsQueryable()); var claim = new Claim("test", "id-123"); var identity = new Mock <ClaimsIdentity>(); identity.Setup(i => i.FindFirst(It.IsAny <string>())).Returns(claim); var principal = new Mock <IPrincipal>(); principal.Setup(p => p.Identity).Returns(identity.Object); var context = new Mock <ControllerContext>(); context.Setup(c => c.HttpContext.User).Returns(principal.Object); AnswerController controller = new AnswerController(data.Object) { ControllerContext = context.Object }; Answer answer = new Answer(); // Act RedirectToRouteResult result = controller.Index(answer, 13, threadTitle) as RedirectToRouteResult; // Assert Assert.AreEqual(threadTitle, result.RouteValues["title"]); }
public void AnswerController_Index_Post_ShouldCallDataSaveChanges() { // Arrange var data = new Mock <IUowData>(); data.Setup(d => d.Answers.All()).Returns(AnswersCollection().AsQueryable()); var claim = new Claim("test", "id-123"); var identity = new Mock <ClaimsIdentity>(); identity.Setup(i => i.FindFirst(It.IsAny <string>())).Returns(claim); var principal = new Mock <IPrincipal>(); principal.Setup(p => p.Identity).Returns(identity.Object); var context = new Mock <ControllerContext>(); context.Setup(c => c.HttpContext.User).Returns(principal.Object); AnswerController controller = new AnswerController(data.Object) { ControllerContext = context.Object }; Answer answer = new Answer(); // Act var result = controller.Index(answer, 1, "ThreadTitle") as ViewResult; // Assert data.Verify(d => d.SaveChanges(), Times.Once); }
public void ShouldGetAnswers() { var expectedTitle = "My expected Answers"; var answers_controller = new AnswerController(); var result = (AnswerViewModel)answers_controller.Index().ViewData.Model; Assert.That(result.AnswerTitle, Is.EqualTo(expectedTitle)); }
public void ShouldGetAnswer_Content() { var expected_answer = "Cuba"; var controller = new AnswerController(); var result = (AnswerViewModel)controller.Index().ViewData.Model; Assert.That(result.AnswerText, Is.EqualTo(expected_answer)); }
public void AnswerController_Index_ShouldReturnCorrectPartialView() { // Arrange var data = new Mock <IUowData>(); AnswerController controller = new AnswerController(data.Object); // Act var result = controller.Index() as PartialViewResult; // Assert Assert.AreEqual("_Answer", result.ViewName); }
public void AnswerController_Index_Post_ShouldReturnBadRequestWhenIdIsNull() { // Arrange var data = new Mock <IUowData>(); AnswerController controller = new AnswerController(data.Object); Answer answer = new Answer(); // Act var result = controller.Index(answer, null, "ThreadTitle") as HttpStatusCodeResult; // Assert Assert.AreEqual(400, result.StatusCode); }
public void Index_WhenCalled_ShouldReturnQuestionWithAnswerCountInViewModel() { var question1 = _context.AddTestQuestionToDatabase("Question 1"); var question2 = _context.AddTestQuestionToDatabase("Question 2"); var answer = _context.AddTestAnswerToDatabase(question1.Id); var result = _controller.Index(); var viewModel = (result as ViewResult).Model as QuestionAnswerCountViewModel; Assert.That(viewModel.QuestionsWithAnswerCount.Count, Is.EqualTo(2)); Assert.That(viewModel.QuestionsWithAnswerCount.Single(kvp => kvp.Key.Id == question1.Id).Value, Is.EqualTo(1)); Assert.That(viewModel.QuestionsWithAnswerCount.Single(kvp => kvp.Key.Id == question2.Id).Value, Is.EqualTo(0)); }
public void Index_WhenCalled_ReturnViewResult() { var result = _controller.Index(); Assert.That(result, Is.TypeOf <ViewResult>()); }