Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
 public ActionResult Index()
 {
     var viewModel = new CalculatorViewModel();
     viewModel.N = 0;
     return View(viewModel);
 }