Exemplo n.º 1
0
        public void TestDefaultRoute()
        {
            // Arrange
            // Mock the HTTP request also
            HttpRequestMock mockRequest = new HttpRequestMock();
            Uri             mockUrl     = new Uri("http://www.cosmomonger.com/");

            mockRequest.Expect(r => r.Url)
            .Returns(mockUrl);
            mockRequest.Expect(r => r.HttpMethod)
            .Returns("GET");
            mockRequest.Expect(r => r.AppRelativeCurrentExecutionFilePath)
            .Returns("~/");

            HttpContextMock mockHttpContext = new HttpContextMock();

            mockHttpContext.Expect(c => c.Request)
            .Returns(mockRequest.Object);

            // Act
            RouteCollection routeCollection = new RouteCollection();

            MvcApplication.RegisterRoutes(routeCollection);
            RouteData routeData = routeCollection.GetRouteData(mockHttpContext.Object);

            // Assert
            Assert.AreEqual("Home", routeData.Values["controller"], "Default controller is HomeController");
            Assert.AreEqual("Index", routeData.Values["action"], "Default action is Index");
            Assert.AreEqual(String.Empty, routeData.Values["id"], "Default Id is empty string");

            /*
             * // This code can almost render a view
             * HttpRequestMock mockRequest = new HttpRequestMock();
             * Uri mockUrl = new Uri("http://www.cosmomonger.com/");
             * mockRequest.Expect(r => r.Url)
             *  .Returns(mockUrl);
             * mockRequest.Expect(r => r.HttpMethod)
             *  .Returns("GET");
             * mockRequest.Expect(r => r.AppRelativeCurrentExecutionFilePath)
             *  .Returns("~/");
             *
             * HttpContextMock mockHttpContext = new HttpContextMock();
             * mockHttpContext.Expect(c => c.Request)
             *  .Returns(mockRequest.Object);
             *
             * RouteCollection routeCollection = new RouteCollection();
             * MvcApplication.RegisterRoutes(routeCollection);
             * RouteData routeData = routeCollection.GetRouteData(mockHttpContext.Object);
             *
             * ControllerContext controllerContext = new ControllerContext(mockHttpContext.Object, routeData, controller);
             * StringWriter writer = new StringWriter();
             * result.ExecuteResult(controllerContext);
             * ViewContext viewContext = new ViewContext(controllerContext, result.View, result.ViewData, result.TempData);
             * result.View.Render(viewContext, writer);
             * Assert.Fail(writer.ToString());
             */
        }
Exemplo n.º 2
0
        public void SendVerificationCodeSuccessful()
        {
            // Arrange
            string testUserName = "******";

            Mock <User> mockUserModel = new Mock <User>();
            Mock <CosmoMongerMembershipUser> mockUser = new Mock <CosmoMongerMembershipUser>(mockUserModel.Object);

            mockUser.Expect(m => m.SendVerificationCode(It.IsRegex("http://www.cosmomonger.com/Account/VerifyEmail?username=TestUser&verificationCode=.*")))
            .AtMostOnce().Verifiable();

            Mock <MembershipProvider> mockMembership = new Mock <MembershipProvider>();

            mockMembership.Expect <MembershipUser>(m => m.GetUser(testUserName, false))
            .Returns(mockUser.Object).AtMostOnce().Verifiable();

            // Mock the HTTP request also
            HttpRequestMock mockRequest = new HttpRequestMock();
            Uri             mockUrl     = new Uri("http://www.cosmomonger.com/Account/SendVerificationCode?username=TestUser");

            mockRequest.Expect(r => r.Url)
            .Returns(mockUrl);
            mockRequest.Expect(r => r.HttpMethod)
            .Returns("GET");
            mockRequest.Expect(r => r.AppRelativeCurrentExecutionFilePath)
            .Returns("~/Account/");

            HttpContextMock mockHttpContext = new HttpContextMock();

            mockHttpContext.Expect(c => c.Request)
            .Returns(mockRequest.Object);

            RouteCollection routeCollection = new RouteCollection();

            MvcApplication.RegisterRoutes(routeCollection);
            RouteData route = routeCollection.GetRouteData(mockHttpContext.Object);

            AccountController controller = new AccountController(mockMembership.Object);

            controller.ControllerContext = new ControllerContext(mockHttpContext.Object, route, new Mock <ControllerBase>().Object);
            controller.Url = new UrlHelper(new RequestContext(mockHttpContext.Object, route));

            // Act
            ViewResult result = (ViewResult)controller.SendVerificationCode(testUserName);

            // Assert
            Assert.That(result.ViewName, Is.EqualTo("SentVerificationCode"), "Should have returned the SentVerificationCode view");
        }