public ActionResult Index(CalculatorViewModel viewModel) { if (!ModelState.IsValid) { viewModel.Result = string.Empty; } else { try { viewModel.Result = this.calculator.GetNthValue(viewModel.N); } catch { ModelState.AddModelError("Global", "An unexpected error occurred"); } } return View(viewModel); }
public void Post_WithModelError_ClearsResult_Test() { // Setup var viewModel = new CalculatorViewModel { N = 100001, Result = "test" }; target.ModelState.AddModelError("Test", "Some error message"); // Act var result = target.Index(viewModel) as ViewResult; viewModel = result.Model as CalculatorViewModel; // Assert Assert.IsNotNull(result); Assert.IsNotNull(viewModel); Assert.AreEqual(100001, viewModel.N); Assert.AreEqual(string.Empty, viewModel.Result); mockFibonacciCalculator.Verify(o => o.GetNthValue(It.IsAny<int>()), Times.Never); }
public void Post_WithException_DisplaysError_Test() { // Setup var viewModel = new CalculatorViewModel { N = 1, Result = "1" }; mockFibonacciCalculator.Setup(o => o.GetNthValue(It.IsAny<int>())).Throws(new Exception()); // Act var result = target.Index(viewModel) as ViewResult; viewModel = result.Model as CalculatorViewModel; // Assert Assert.IsFalse(target.ModelState.IsValid); Assert.IsNotNull(result); Assert.IsNotNull(viewModel); Assert.AreEqual(1, viewModel.N); Assert.AreEqual("1", viewModel.Result); }
public void Post_WithValidN_Test() { // Setup var viewModel = new CalculatorViewModel { N = 1 }; mockFibonacciCalculator.Setup(o => o.GetNthValue(1)).Returns("1").Verifiable(); // Act var result = target.Index(viewModel) as ViewResult; viewModel = result.Model as CalculatorViewModel; // Assert mockFibonacciCalculator.Verify(); Assert.IsNotNull(result); Assert.IsNotNull(viewModel); Assert.AreEqual(1, viewModel.N); Assert.AreEqual("1", viewModel.Result); }
public ActionResult Index() { var viewModel = new CalculatorViewModel(); viewModel.N = 0; return View(viewModel); }