Example #1
0
        public void ShouldBuildIndexViewWithCustomersOrderByName()
        {
            ICustomerAdapter adapter = Substitute.For <ICustomerAdapter>();

            CustomerData[] customers = new CustomerData[]
            {
                new CustomerData()
                {
                    Name = "Gary"
                },
                new CustomerData()
                {
                    Name = "Louis"
                },
                new CustomerData()
                {
                    Name = "Bob"
                }
            };
            adapter.GetCustomers().Returns(customers);

            using (ClassicController controller = new ClassicController(adapter))
            {
                ActionResult result = controller.Index();

                adapter.Received().GetCustomers();

                CustomerData[] model    = ActionResultHelper.AssertViewWithModel <CustomerData[]>(result, controller.Views.Index);
                string[]       names    = model.Select(customer => customer.Name).ToArray();
                string[]       expected = new string[] { "Bob", "Gary", "Louis" };
                CollectionAssert.AreEqual(expected, names, "The customers were not ordered by name.");
            }
        }
Example #2
0
        public void ShouldRedisplayViewIfCreateValidationFails()
        {
            ICustomerAdapter adapter = Substitute.For <ICustomerAdapter>();

            using (ClassicController controller = new ClassicController(adapter))
            {
                controller.ModelState.AddModelError("txtName", "You must provide a customer name.");

                CustomerData data   = new CustomerData();
                ActionResult result = controller.Create(data);

                CustomerData model = ActionResultHelper.AssertViewWithModel <CustomerData>(result, controller.Views.Create);
                Assert.AreSame(data, model, "The customer data was not set as the model.");
            }
        }
Example #3
0
        public void ShouldRedisplayViewWhenEditValidationFails()
        {
            ICustomerAdapter adapter = Substitute.For <ICustomerAdapter>();

            using (ClassicController controller = new ClassicController(adapter))
            {
                controller.ModelState.AddModelError("txtName", "You must provide a name for the customer.");

                CustomerData data   = new CustomerData();
                ActionResult result = controller.Edit(data);

                CustomerData model = ActionResultHelper.AssertViewWithModel <CustomerData>(result, controller.Views.Edit);
                Assert.AreSame(data, model, "The model was not passed to the view.");
            }
        }
Example #4
0
        public void ShouldDisplayDefaultErrorMessageIfErrorNotProvided()
        {
            IUrlHelper helper    = Substitute.For <IUrlHelper>();
            string     returnUrl = "/return/url";

            helper.Action(Arg.Any <ActionResult>()).Returns(returnUrl);
            using (ErrorController controller = new ErrorController(helper))
            {
                ActionResult result = controller.Index();

                ErrorData model = ActionResultHelper.AssertViewWithModel <ErrorData>(result, controller.Views.Index);

                Assert.AreEqual("An error occurred while processing your request.", model.ErrorMessage, "The wrong error message was set.");
                Assert.AreEqual(returnUrl, model.ReturnUrl, "The wrong return URL was set.");
            }
        }
Example #5
0
        public void ShouldRetrieveCustomerForDelete()
        {
            ICustomerAdapter adapter = Substitute.For <ICustomerAdapter>();
            CustomerData     data    = new CustomerData();

            adapter.GetCustomer(Arg.Any <string>()).Returns(data);

            using (ClassicController controller = new ClassicController(adapter))
            {
                string       customerId = Guid.NewGuid().ToString("N");
                ActionResult result     = controller.Delete(customerId);

                CustomerData model = ActionResultHelper.AssertViewWithModel <CustomerData>(result, controller.Views.Delete);

                adapter.Received().GetCustomer(customerId);

                Assert.AreSame(data, model, "The customer data was not passed to the view.");
            }
        }
Example #6
0
        public void ShouldUseGivenErrorMessageAndReturnUrlIfProvided()
        {
            IUrlHelper helper = Substitute.For <IUrlHelper>();

            helper.IsSafe(Arg.Any <String>()).Returns(true);
            using (ErrorController controller = new ErrorController(helper))
            {
                ErrorData error = new ErrorData()
                {
                    ErrorMessage = "Custom error message",
                    ReturnUrl    = "/return/url",
                };
                ActionResult result = controller.Index(error);

                ErrorData model = ActionResultHelper.AssertViewWithModel <ErrorData>(result, controller.Views.Index);

                Assert.AreEqual(error.ErrorMessage, model.ErrorMessage, "The wrong error message was set.");
                Assert.AreEqual(error.ReturnUrl, model.ReturnUrl, "The wrong return URL was set.");
            }
        }
Example #7
0
        public void ShouldChangeReturnUrlIfNotSafe()
        {
            IUrlHelper helper = Substitute.For <IUrlHelper>();

            helper.IsSafe(Arg.Any <string>()).Returns(false);
            string returnUrl = "/return/url";

            helper.Action(Arg.Any <ActionResult>()).Returns(returnUrl);
            using (ErrorController controller = new ErrorController(helper))
            {
                ErrorData error = new ErrorData()
                {
                    ErrorMessage = "Custom error message",
                    ReturnUrl    = @"http://www.google.com",
                };
                ActionResult result = controller.Index(error);

                ErrorData model = ActionResultHelper.AssertViewWithModel <ErrorData>(result, controller.Views.Index);

                Assert.AreEqual(error.ErrorMessage, model.ErrorMessage, "The wrong error message was set.");
                Assert.AreEqual(returnUrl, model.ReturnUrl, "The wrong return URL was set.");
            }
        }