Ejemplo n.º 1
0
        public void ShouldCreateAndReturnCustomer()
        {
            ICustomerAdapter adapter = Substitute.For <ICustomerAdapter>();
            CustomerData     added   = new CustomerData();

            adapter.AddCustomer(Arg.Any <CustomerData>()).Returns(added);
            using (KnockoutController controller = new KnockoutController(adapter))
            {
                CustomerData data   = new CustomerData();
                ActionResult result = controller.Create(data);

                CustomerData model = ActionResultHelper.AssertJsonWithHttpStatusCode <CustomerData>(result, HttpStatusCode.Created);
                Assert.AreSame(added, model, "The added customer was not returned.");

                adapter.Received().AddCustomer(data);
            }
        }
Ejemplo n.º 2
0
        public void ShouldDeleteCustomerAndRedirectToIndex()
        {
            ICustomerAdapter adapter = Substitute.For <ICustomerAdapter>();

            using (ClassicController controller = new ClassicController(adapter))
            {
                CustomerData data = new CustomerData()
                {
                    CustomerId = Guid.NewGuid().ToString("N")
                };
                ActionResult result = controller.Delete(data);

                adapter.Received().RemoveCustomer(data.CustomerId);

                ActionResultHelper.AssertRedirected(result, controller.Name, controller.ActionNames.Index);
            }
        }
Ejemplo n.º 3
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.");
            }
        }
Ejemplo n.º 4
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.");
            }
        }
Ejemplo n.º 5
0
        public void ShouldLoadCustomers()
        {
            ICustomerAdapter           adapter = Substitute.For <ICustomerAdapter>();
            IEnumerable <CustomerData> data    = new List <CustomerData>()
            {
                new CustomerData()
            };

            adapter.GetCustomers().Returns(data);
            using (KnockoutController controller = new KnockoutController(adapter))
            {
                ActionResult result = controller.Load();

                CustomerData[] model = ActionResultHelper.AssertJson <CustomerData[]>(result);
                Assert.AreEqual(1, model.Length, "There should have been one customer.");
                Assert.AreSame(data.First(), model[0], "The wrong customer was returned.");

                adapter.Received().GetCustomers();
            }
        }
Ejemplo n.º 6
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.");
            }
        }