コード例 #1
0
        public void ControllerFileStream_InvokedInUnitTests()
        {
            // Arrange
            var content = "<html>CreatedBody</html>";
            var contentType = "text/html";
            var fileName = "Created.html";
            var mockHttpContext = new Mock<HttpContext>();
            mockHttpContext.Setup(x => x.Response.RegisterForDispose(It.IsAny<IDisposable>()));

            var controller = new TestabilityController();
            controller.ControllerContext.HttpContext = mockHttpContext.Object;

            // Act
            var result = controller.FileStream_Action(content, contentType, fileName);

            // Assert
            Assert.NotNull(result);

            var fileStreamResult = Assert.IsType<FileStreamResult>(result);
            Assert.Equal(contentType, fileStreamResult.ContentType.ToString());
            Assert.Equal(fileName ?? string.Empty, fileStreamResult.FileDownloadName);

            if (content == null)
            {
                Assert.Null(fileStreamResult.FileStream);
            }
            else
            {
                using (var stream = new StreamReader(fileStreamResult.FileStream, Encoding.UTF8))
                {
                    Assert.Equal(content, stream.ReadToEnd());
                }
            }
        }
コード例 #2
0
        public void ActionContextSetters_CanBeUsedWithControllerActionContext()
        {
            // Arrange
            var actionDescriptor = new Mock <ActionDescriptor>();
            var httpContext      = new Mock <HttpContext>();
            var routeData        = new Mock <RouteData>();

            var actionContext = new ActionContext()
            {
                ActionDescriptor = actionDescriptor.Object,
                HttpContext      = httpContext.Object,
                RouteData        = routeData.Object,
            };

            var controller = new TestabilityController();

            // Act
            controller.ActionContext = actionContext;

            // Assert
            Assert.Equal(httpContext.Object, controller.Context);
            Assert.Equal(routeData.Object, controller.RouteData);
            Assert.Equal(actionContext.ModelState, controller.ModelState);
            Assert.Equal(actionDescriptor.Object, actionContext.ActionDescriptor);
        }
コード例 #3
0
        public void ControllerCreatedAtRoute_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();
            var routeName = "RouteName_1";
            var routeValues = new Dictionary<string, object>() { { "route", "sample" } };
            var value = new { Value = "Value_1" };

            // Act
            var result = controller.CreatedAtRoute_Action(routeName, routeValues, value);

            // Assert
            Assert.NotNull(result);

            var createdAtRouteResult = Assert.IsType<CreatedAtRouteResult>(result);
            Assert.Equal(routeName, createdAtRouteResult.RouteName);
            Assert.Single(createdAtRouteResult.RouteValues);
            Assert.Equal("sample", createdAtRouteResult.RouteValues["route"]);
            Assert.Same(value, createdAtRouteResult.Value);

            // Arrange
            controller = new TestabilityController();

            // Act
            result = controller.CreatedAtRoute_Action(null, null, null);

            // Assert
            Assert.NotNull(result);

            createdAtRouteResult = Assert.IsType<CreatedAtRouteResult>(result);
            Assert.Null(createdAtRouteResult.RouteName);
            Assert.Null(createdAtRouteResult.RouteValues);
            Assert.Null(createdAtRouteResult.Value);
        }
コード例 #4
0
        public void ControllerFileContent_InvokedInUnitTests()
        {
            // Arrange
            var content     = "<html>CreatedBody</html>";
            var contentType = "text/html";
            var fileName    = "Created.html";
            var controller  = new TestabilityController();

            // Act
            var result = controller.FileContent_Action(content, contentType, fileName);

            // Assert
            Assert.NotNull(result);

            var fileContentResult = Assert.IsType <FileContentResult>(result);

            Assert.Equal(contentType, fileContentResult.ContentType.ToString());
            Assert.Equal(fileName ?? string.Empty, fileContentResult.FileDownloadName);

            if (content == null)
            {
                Assert.Null(fileContentResult.FileContents);
            }
            else
            {
                Assert.Equal(content, Encoding.UTF8.GetString(fileContentResult.FileContents));
            }
        }
コード例 #5
0
        public void ControllerHttpBadRequestObject_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();
            var error      = new { Error = "Error Message" };

            // Act
            var result = controller.HttpBadRequestObject_Action(error);

            // Assert
            Assert.NotNull(result);

            var httpBadRequest = Assert.IsType <BadRequestObjectResult>(result);

            Assert.Equal(StatusCodes.Status400BadRequest, httpBadRequest.StatusCode);
            Assert.Same(error, httpBadRequest.Value);

            // Arrange
            controller = new TestabilityController();

            // Act
            result = controller.HttpBadRequestObject_Action(null);

            // Assert
            Assert.NotNull(result);

            httpBadRequest = Assert.IsType <BadRequestObjectResult>(result);
            Assert.Equal(StatusCodes.Status400BadRequest, httpBadRequest.StatusCode);
            Assert.Null(httpBadRequest.Value);
        }
コード例 #6
0
        public void ControllerJson_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();
            var model      = new MyModel()
            {
                Property1 = "Property_1"
            };

            // Act
            var result = controller.Json_Action(model);

            // Assert
            Assert.NotNull(result);

            var jsonResult = Assert.IsType <JsonResult>(result);

            Assert.NotNull(jsonResult.Value);
            Assert.Same(model, jsonResult.Value);
            Assert.IsType <MyModel>(jsonResult.Value);

            // Arrange
            controller = new TestabilityController();

            // Act
            result = controller.Json_Action(null);

            // Assert
            Assert.NotNull(result);

            jsonResult = Assert.IsType <JsonResult>(result);
            Assert.Null(jsonResult.Value);
        }
コード例 #7
0
        public void ControllerJsonWithSerializerSettings_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();
            var model      = new MyModel()
            {
                Property1 = "Property_1"
            };
            var serializerSettings = new JsonSerializerSettings();

            // Act
            var result = controller.JsonWithSerializerSettings_Action(model, serializerSettings);

            // Assert
            Assert.NotNull(result);

            var jsonResult = Assert.IsType <JsonResult>(result);

            Assert.NotNull(jsonResult.Value);
            Assert.Same(model, jsonResult.Value);
            Assert.IsType <MyModel>(jsonResult.Value);
            var jsonFormatter = jsonResult.Formatter as JsonOutputFormatter;

            Assert.Same(serializerSettings, jsonFormatter.SerializerSettings);
        }
コード例 #8
0
        public void ControllerFileStream_InvokedInUnitTests(string content, string contentType, string fileName)
        {
            // Arrange
            var mockHttpContext = new Mock <DefaultHttpContext>();

            mockHttpContext.Setup(x => x.Response.OnResponseCompleted(It.IsAny <Action <object> >(), It.IsAny <object>()));
            var controller = new TestabilityController()
            {
                ActionContext = new ActionContext(mockHttpContext.Object, new RouteData(), new ActionDescriptor())
            };

            // Act
            var result = controller.FileStream_Action(content, contentType, fileName);

            // Assert
            Assert.NotNull(result);

            var fileStreamResult = Assert.IsType <FileStreamResult>(result);

            Assert.Equal(contentType, fileStreamResult.ContentType);
            Assert.Equal(fileName ?? string.Empty, fileStreamResult.FileDownloadName);

            if (content == null)
            {
                Assert.Null(fileStreamResult.FileStream);
            }
            else
            {
                using (var stream = new StreamReader(fileStreamResult.FileStream, Encoding.UTF8))
                {
                    Assert.Equal(content, stream.ReadToEnd());
                }
            }
        }
コード例 #9
0
        public void ControllerFileStream_InvokedInUnitTests(string content, string contentType, string fileName)
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.FileStream_Action(content, contentType, fileName);

            // Assert
            Assert.NotNull(result);

            var fileStreamResult = Assert.IsType <FileStreamResult>(result);

            Assert.Equal(contentType, fileStreamResult.ContentType);
            Assert.Equal(fileName ?? string.Empty, fileStreamResult.FileDownloadName);

            if (content == null)
            {
                Assert.Null(fileStreamResult.FileStream);
            }
            else
            {
                using (var stream = new StreamReader(fileStreamResult.FileStream, Encoding.UTF8))
                {
                    Assert.Equal(content, stream.ReadToEnd());
                }
            }
        }
コード例 #10
0
        public void ControllerContextSetter_CanBeUsedWithControllerActionContext()
        {
            // Arrange
            var actionDescriptor = new ControllerActionDescriptor();
            var httpContext      = new DefaultHttpContext();
            var routeData        = new RouteData();

            var controllerContext = new ControllerContext()
            {
                ActionDescriptor = actionDescriptor,
                HttpContext      = httpContext,
                RouteData        = routeData,
            };

            var controller = new TestabilityController();

            // Act
            controller.ControllerContext = controllerContext;

            // Assert
            Assert.Same(httpContext, controller.HttpContext);
            Assert.Same(routeData, controller.RouteData);
            Assert.Equal(controllerContext.ModelState, controller.ModelState);
            Assert.Same(actionDescriptor, controllerContext.ActionDescriptor);
        }
コード例 #11
0
        public void ControllerRedirectToRoute_InvokedInUnitTests()
        {
            // Arrange
            var controller  = new TestabilityController();
            var routeName   = "RouteName_1";
            var routeValues = new Dictionary <string, object>()
            {
                { "route", "sample" }
            };

            // Act
            var result = controller.RedirectToRoute_Action(routeName, routeValues);

            // Assert
            Assert.NotNull(result);

            var redirectToRouteResult = Assert.IsType <RedirectToRouteResult>(result);

            Assert.Equal(routeName, redirectToRouteResult.RouteName);
            Assert.Single(redirectToRouteResult.RouteValues);
            Assert.Equal("sample", redirectToRouteResult.RouteValues["route"]);

            // Arrange
            controller = new TestabilityController();

            // Act
            result = controller.RedirectToRoute_Action(null, null);

            // Assert
            Assert.NotNull(result);

            redirectToRouteResult = Assert.IsType <RedirectToRouteResult>(result);
            Assert.Null(redirectToRouteResult.RouteName);
            Assert.Empty(redirectToRouteResult.RouteValues);
        }
コード例 #12
0
        public void ControllerPartialView_InvokedInUnitTests(object model, string viewName)
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.PartialView_Action(viewName, model);

            // Assert
            Assert.NotNull(result);

            var viewResult = Assert.IsType <PartialViewResult>(result);

            Assert.Equal(viewName, viewResult.ViewName);
            Assert.NotNull(viewResult.ViewData);
            Assert.Same(model, viewResult.ViewData.Model);
            Assert.Same(controller.ViewData, viewResult.ViewData);
            Assert.Same(controller.TempData, viewResult.TempData);

            if (model != null)
            {
                Assert.IsType(model.GetType(), viewResult.ViewData.Model);
                Assert.NotNull(viewResult.ViewData.Model);
            }
        }
コード例 #13
0
        public void ControllerActionContext_ReturnsNotNull()
        {
            // Arrange && Act
            var controller = new TestabilityController();

            // Assert
            Assert.NotNull(controller.ActionContext);
            Assert.NotNull(controller.ActionContext.ModelState);
            Assert.Null(controller.ActionContext.ActionDescriptor);
            Assert.Null(controller.ActionContext.HttpContext);
            Assert.Null(controller.ActionContext.RouteData);
        }
コード例 #14
0
        public void ViewComponent_WithType()
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.ViewComponent(typeof(TagCloudViewComponent));

            // Assert
            Assert.NotNull(result);

            Assert.Equal(typeof(TagCloudViewComponent), result.ViewComponentType);
        }
コード例 #15
0
        public void ViewComponent_WithName()
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.ViewComponent("TagCloud");

            // Assert
            Assert.NotNull(result);

            Assert.Equal("TagCloud", result.ViewComponentName);
        }
コード例 #16
0
        public void ActionContextDefaultConstructor_CanBeUsedForControllerActionContext()
        {
            // Arrange
            var actionContext = new ActionContext();
            var controller    = new TestabilityController();

            // Act
            controller.ActionContext = actionContext;

            // Assert
            Assert.Equal(actionContext.HttpContext, controller.Context);
            Assert.Equal(actionContext.RouteData, controller.RouteData);
            Assert.Equal(actionContext.ModelState, controller.ModelState);
        }
コード例 #17
0
        public void ControllerHttpNotFound_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.HttpNotFound_Action();

            // Assert
            Assert.NotNull(result);

            var httpNotFoundResult = Assert.IsType<NotFoundResult>(result);
            Assert.Equal(StatusCodes.Status404NotFound, httpNotFoundResult.StatusCode);
        }
コード例 #18
0
        public void ViewComponent_WithArguments()
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.ViewComponent(typeof(TagCloudViewComponent), new { Arg1 = "Hi", Arg2 = "There" });

            // Assert
            Assert.NotNull(result);

            Assert.Equal(typeof(TagCloudViewComponent), result.ViewComponentType);
            Assert.Equal(new { Arg1 = "Hi", Arg2 = "There" }, result.Arguments);
        }
コード例 #19
0
        public void ControllerHttpBadRequest_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.HttpBadRequest_Action();

            // Assert
            Assert.NotNull(result);

            var httpBadRequest = Assert.IsType <BadRequestResult>(result);

            Assert.Equal(400, httpBadRequest.StatusCode);
        }
コード例 #20
0
        public void Problem_Works()
        {
            // Arrange
            var detail     = "Some random error";
            var controller = new TestabilityController();

            // Act
            var result = controller.Problem(detail);

            // Assert
            var badRequest     = Assert.IsType <ObjectResult>(result);
            var problemDetails = Assert.IsType <ProblemDetails>(badRequest.Value);

            Assert.Equal(detail, problemDetails.Detail);
        }
コード例 #21
0
        public void ControllerCreated_InvokedInUnitTests(string uri, string content)
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.Created_Action(uri, content);

            // Assert
            Assert.NotNull(result);

            var createdResult = Assert.IsType<CreatedResult>(result);
            Assert.Equal(uri, createdResult.Location);
            Assert.Equal(content, createdResult.Value);
            Assert.Equal(StatusCodes.Status201Created, createdResult.StatusCode);
        }
コード例 #22
0
        public void ContextModelState_ShouldBeSameAsViewDataAndControllerModelState()
        {
            // Arrange
            var controller1 = new TestabilityController();
            var controller2 = new TestabilityController();

            // Act
            controller2.ControllerContext = new ControllerContext();

            // Assert
            Assert.Equal(controller1.ModelState, controller1.ControllerContext.ModelState);
            Assert.Equal(controller1.ModelState, controller1.ViewData.ModelState);

            Assert.Equal(controller1.ControllerContext.ModelState, controller2.ModelState);
            Assert.Equal(controller1.ControllerContext.ModelState, controller2.ControllerContext.ModelState);
            Assert.Equal(controller1.ControllerContext.ModelState, controller2.ViewData.ModelState);
        }
コード例 #23
0
        public void ControllerContent_InvokedInUnitTests(string content, string contentType, Encoding encoding)
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.Content_Action(content, contentType, encoding);

            // Assert
            Assert.NotNull(result);

            var contentResult = Assert.IsType <ContentResult>(result);

            Assert.Equal(content, contentResult.Content);
            Assert.Equal(contentType, contentResult.ContentType);
            Assert.Equal(encoding, contentResult.ContentEncoding);
        }
コード例 #24
0
        public void ControllerContent_InvokedInUnitTests()
        {
            // Arrange
            var content = "Content_1";
            var contentType = "text/asp";
            var encoding = Encoding.ASCII;
            var controller = new TestabilityController();

            // Act
            var result = controller.Content_Action(content, contentType, encoding);

            // Assert
            Assert.NotNull(result);

            var contentResult = Assert.IsType<ContentResult>(result);
            Assert.Equal(content, contentResult.Content);
            Assert.Equal("text/asp; charset=us-ascii", contentResult.ContentType.ToString());
            Assert.Equal(encoding, MediaType.GetEncoding(contentResult.ContentType));
        }
コード例 #25
0
        public void ControllerContent_InvokedInUnitTests()
        {
            // Arrange
            var content = "Content_1";
            var contentType = "text/asp";
            var encoding = Encoding.ASCII;
            var controller = new TestabilityController();

            // Act
            var result = controller.Content_Action(content, contentType, encoding);

            // Assert
            Assert.NotNull(result);

            var contentResult = Assert.IsType<ContentResult>(result);
            Assert.Equal(content, contentResult.Content);
            Assert.Equal("text/asp; charset=us-ascii", contentResult.ContentType.ToString());
            Assert.Equal(encoding, contentResult.ContentType.Encoding);
        }
コード例 #26
0
        public void ValidationProblem_Works()
        {
            // Arrange
            var detail     = "Some random error";
            var controller = new TestabilityController();

            // Act
            controller.ModelState.AddModelError("some-key", "some-error");
            var result = controller.ValidationProblem(detail);

            // Assert
            var badRequest = Assert.IsType <ObjectResult>(result);
            var validationProblemDetails = Assert.IsType <ValidationProblemDetails>(badRequest.Value);

            Assert.Equal(detail, validationProblemDetails.Detail);
            var error = Assert.Single(validationProblemDetails.Errors);

            Assert.Equal("some-key", error.Key);
            Assert.Equal(new[] { "some-error" }, error.Value);
        }
コード例 #27
0
        public void ControllerAcceptedAtAction_InvokedInUnitTests()
        {
            // Arrange
            var controller     = new TestabilityController();
            var actionName     = "ActionName_1";
            var controllerName = "ControllerName_1";
            var routeValues    = new Dictionary <string, object>()
            {
                { "route", "sample" }
            };
            var value = new { Value = "Value_1" };

            // Act
            var result = controller.AcceptedAtAction_Action(actionName, controllerName, routeValues, value);

            // Assert
            Assert.NotNull(result);

            var acceptedAtActionResult = Assert.IsType <AcceptedAtActionResult>(result);

            Assert.Equal(actionName, acceptedAtActionResult.ActionName);
            Assert.Equal(controllerName, acceptedAtActionResult.ControllerName);
            Assert.Single(acceptedAtActionResult.RouteValues);
            Assert.Equal("sample", acceptedAtActionResult.RouteValues["route"]);
            Assert.Same(value, acceptedAtActionResult.Value);

            // Arrange
            controller = new TestabilityController();

            // Act
            result = controller.AcceptedAtAction_Action(null, null, null, null);

            // Assert
            Assert.NotNull(result);

            acceptedAtActionResult = Assert.IsType <AcceptedAtActionResult>(result);
            Assert.Null(acceptedAtActionResult.ActionName);
            Assert.Null(acceptedAtActionResult.ControllerName);
            Assert.Null(acceptedAtActionResult.Value);
            Assert.Null(acceptedAtActionResult.RouteValues);
        }
コード例 #28
0
        public void ControllerRedirect_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();
            var url = "http://contoso.com";

            // Act
            var result = controller.Redirect_Action(url);

            // Assert
            Assert.NotNull(result);

            var redirectResult = Assert.IsType<RedirectResult>(result);
            Assert.Equal(url, redirectResult.Url);

            // Arrange
            controller = new TestabilityController();

            // Act && Assert
            Assert.Throws<ArgumentException>(() => controller.Redirect_Action(null));
        }
コード例 #29
0
        public void ControllerPartialView_InvokedInUnitTests(object model, string viewName)
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.PartialView_Action(viewName, model);

            // Assert
            Assert.NotNull(result);

            var viewResult = Assert.IsType<PartialViewResult>(result);
            Assert.Equal(viewName, viewResult.ViewName);
            Assert.NotNull(viewResult.ViewData);
            Assert.Same(model, viewResult.ViewData.Model);
            Assert.Same(controller.ViewData, viewResult.ViewData);
            Assert.Same(controller.TempData, viewResult.TempData);

            if (model != null)
            {
                Assert.IsType(model.GetType(), viewResult.ViewData.Model);
                Assert.NotNull(viewResult.ViewData.Model);
            }
        }
コード例 #30
0
        public void ControllerJsonWithSerializerSettings_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();
            var model = new MyModel() { Property1 = "Property_1" };
            var serializerSettings = new JsonSerializerSettings();

            // Act
            var result = controller.JsonWithSerializerSettings_Action(model, serializerSettings);

            // Assert
            Assert.NotNull(result);

            var jsonResult = Assert.IsType<JsonResult>(result);
            Assert.NotNull(jsonResult.Value);
            Assert.Same(model, jsonResult.Value);
            Assert.IsType<MyModel>(jsonResult.Value);
        }
コード例 #31
0
        public void ControllerJson_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();
            var model = new MyModel() { Property1 = "Property_1" };

            // Act
            var result = controller.Json_Action(model);

            // Assert
            Assert.NotNull(result);

            var jsonResult = Assert.IsType<JsonResult>(result);
            Assert.NotNull(jsonResult.Value);
            Assert.Same(model, jsonResult.Value);
            Assert.IsType<MyModel>(jsonResult.Value);

            // Arrange
            controller = new TestabilityController();

            // Act
            result = controller.Json_Action(null);

            // Assert
            Assert.NotNull(result);

            jsonResult = Assert.IsType<JsonResult>(result);
            Assert.Null(jsonResult.Value);
        }
コード例 #32
0
        public void ControllerHttpBadRequest_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.HttpBadRequest_Action();

            // Assert
            Assert.NotNull(result);

            var httpBadRequest = Assert.IsType<BadRequestResult>(result);
            Assert.Equal(StatusCodes.Status400BadRequest, httpBadRequest.StatusCode);
        }
コード例 #33
0
        public void ControllerHttpNotFoundObject_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.HttpNotFoundObject_Action("Test Content");

            // Assert
            Assert.NotNull(result);

            var httpNotFoundObjectResult = Assert.IsType<HttpNotFoundObjectResult>(result);
            Assert.Equal(StatusCodes.Status404NotFound, httpNotFoundObjectResult.StatusCode);
            Assert.Equal("Test Content", httpNotFoundObjectResult.Value);
        }
コード例 #34
0
        public void ViewComponent_WithType()
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.ViewComponent(typeof(TagCloudViewComponent));

            // Assert
            Assert.NotNull(result);

            Assert.Equal(typeof(TagCloudViewComponent), result.ViewComponentType);
        }
コード例 #35
0
        public void ControllerHttpBadRequestObject_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();
            var error = new { Error = "Error Message" };

            // Act
            var result = controller.HttpBadRequestObject_Action(error);

            // Assert
            Assert.NotNull(result);

            var httpBadRequest = Assert.IsType<BadRequestObjectResult>(result);
            Assert.Equal(StatusCodes.Status400BadRequest, httpBadRequest.StatusCode);
            Assert.Same(error, httpBadRequest.Value);

            // Arrange
            controller = new TestabilityController();

            // Act
            result = controller.HttpBadRequestObject_Action(null);

            // Assert
            Assert.NotNull(result);

            httpBadRequest = Assert.IsType<BadRequestObjectResult>(result);
            Assert.Equal(StatusCodes.Status400BadRequest, httpBadRequest.StatusCode);
            Assert.Null(httpBadRequest.Value);
        }
コード例 #36
0
        public void ControllerRedirect_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();
            var url = "http://contoso.com";

            // Act
            var result = controller.Redirect_Action(url);

            // Assert
            Assert.NotNull(result);

            var redirectResult = Assert.IsType<RedirectResult>(result);
            Assert.Equal(url, redirectResult.Url);

            // Arrange
            controller = new TestabilityController();

            // Act && Assert
            Assert.Throws<ArgumentException>(() => controller.Redirect_Action(null));
        }
コード例 #37
0
        public void ControllerContextSetter_CanBeUsedWithControllerActionContext()
        {
            // Arrange
            var actionDescriptor = new ControllerActionDescriptor();
            var httpContext = new DefaultHttpContext();
            var routeData = new RouteData();

            var controllerContext = new ControllerContext()
            {
                ActionDescriptor = actionDescriptor,
                HttpContext = httpContext,
                RouteData = routeData,
            };

            var controller = new TestabilityController();

            // Act
            controller.ControllerContext = controllerContext;

            // Assert
            Assert.Same(httpContext, controller.HttpContext);
            Assert.Same(routeData, controller.RouteData);
            Assert.Equal(controllerContext.ModelState, controller.ModelState);
            Assert.Same(actionDescriptor, controllerContext.ActionDescriptor);
        }
コード例 #38
0
        public void ActionContextSetters_CanBeUsedWithControllerActionContext()
        {
            // Arrange
            var actionDescriptor = new Mock<ActionDescriptor>();
            var httpContext = new Mock<HttpContext>();
            var routeData = new Mock<RouteData>();

            var actionContext = new ActionContext()
            {
                ActionDescriptor = actionDescriptor.Object,
                HttpContext = httpContext.Object,
                RouteData = routeData.Object,
            };

            var controller = new TestabilityController();

            // Act
            controller.ActionContext = actionContext;

            // Assert
            Assert.Equal(httpContext.Object, controller.Context);
            Assert.Equal(routeData.Object, controller.RouteData);
            Assert.Equal(actionContext.ModelState, controller.ModelState);
            Assert.Equal(actionDescriptor.Object, actionContext.ActionDescriptor);
        }
コード例 #39
0
        public void ActionContextDefaultConstructor_CanBeUsedForControllerActionContext()
        {
            // Arrange
            var actionContext = new ActionContext();
            var controller = new TestabilityController();

            // Act
            controller.ActionContext = actionContext;

            // Assert
            Assert.Equal(actionContext.HttpContext, controller.Context);
            Assert.Equal(actionContext.RouteData, controller.RouteData);
            Assert.Equal(actionContext.ModelState, controller.ModelState);
        }
コード例 #40
0
        public void ControllerCreated_InvokedInUnitTests(string uri, string content)
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.Created_Action(uri, content);

            // Assert
            Assert.NotNull(result);

            var createdResult = Assert.IsType<CreatedResult>(result);
            Assert.Equal(uri, createdResult.Location);
            Assert.Equal(content, createdResult.Value);
            Assert.Equal(StatusCodes.Status201Created, createdResult.StatusCode);
        }
コード例 #41
0
        public void ActionContextModelState_ShouldBeSameAsViewDataAndControllerModelState()
        {
            // Arrange
            var actionContext = new ActionContext();
            var controller1 = new TestabilityController();
            var controller2 = new TestabilityController();

            // Act
            controller2.ActionContext = actionContext;

            // Assert
            Assert.Equal(controller1.ModelState, controller1.ActionContext.ModelState);
            Assert.Equal(controller1.ModelState, controller1.ViewData.ModelState);

            Assert.Equal(actionContext.ModelState, controller2.ModelState);
            Assert.Equal(actionContext.ModelState, controller2.ActionContext.ModelState);
            Assert.Equal(actionContext.ModelState, controller2.ViewData.ModelState);
        }
コード例 #42
0
        public void ViewComponent_WithArguments()
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.ViewComponent(typeof(TagCloudViewComponent), "Hi", "There");

            // Assert
            Assert.NotNull(result);

            Assert.Equal(typeof(TagCloudViewComponent), result.ViewComponentType);
            Assert.Equal(new object[] { "Hi", "There" }, result.Arguments);
        }
コード例 #43
0
        public void ControllerFileStream_InvokedInUnitTests()
        {
            // Arrange
            var content = "<html>CreatedBody</html>";
            var contentType = "text/html";
            var fileName = "Created.html";
            var mockHttpContext = new Mock<DefaultHttpContext>();
            mockHttpContext.Setup(x => x.Response.RegisterForDispose(It.IsAny<IDisposable>()));
            var controller = new TestabilityController()
            {
                ActionContext = new ActionContext(mockHttpContext.Object, new RouteData(), new ActionDescriptor())
            };

            // Act
            var result = controller.FileStream_Action(content, contentType, fileName);

            // Assert
            Assert.NotNull(result);

            var fileStreamResult = Assert.IsType<FileStreamResult>(result);
            Assert.Equal(contentType, fileStreamResult.ContentType.ToString());
            Assert.Equal(fileName ?? string.Empty, fileStreamResult.FileDownloadName);

            if (content == null)
            {
                Assert.Null(fileStreamResult.FileStream);
            }
            else
            {
                using (var stream = new StreamReader(fileStreamResult.FileStream, Encoding.UTF8))
                {
                    Assert.Equal(content, stream.ReadToEnd());
                }
            }
        }
コード例 #44
0
        public void ControllerRedirectToAction_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();
            var controllerName = "ControllerName_1";
            var actionName = "ActionName_1";
            var routeValues = new Dictionary<string, object>() { { "route", "sample" } };

            // Act
            var result = controller.RedirectToAction_Action(actionName, controllerName, routeValues);

            // Assert
            Assert.NotNull(result);

            var redirectToActionResult = Assert.IsType<RedirectToActionResult>(result);
            Assert.Equal(actionName, redirectToActionResult.ActionName);
            Assert.Equal(controllerName, redirectToActionResult.ControllerName);
            Assert.Single(redirectToActionResult.RouteValues);
            Assert.Equal("sample", redirectToActionResult.RouteValues["route"]);

            // Arrange
            controller = new TestabilityController();

            // Act
            result = controller.RedirectToAction_Action(null, null, null);

            // Assert
            Assert.NotNull(result);

            redirectToActionResult = Assert.IsType<RedirectToActionResult>(result);
            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Null(redirectToActionResult.ActionName);
            Assert.Empty(redirectToActionResult.RouteValues);
        }
コード例 #45
0
        public void ControllerFileContent_InvokedInUnitTests()
        {
            // Arrange
            var content = "<html>CreatedBody</html>";
            var contentType = "text/html";
            var fileName = "Created.html";
            var controller = new TestabilityController();

            // Act
            var result = controller.FileContent_Action(content, contentType, fileName);

            // Assert
            Assert.NotNull(result);

            var fileContentResult = Assert.IsType<FileContentResult>(result);
            Assert.Equal(contentType, fileContentResult.ContentType.ToString());
            Assert.Equal(fileName ?? string.Empty, fileContentResult.FileDownloadName);

            if (content == null)
            {
                Assert.Null(fileContentResult.FileContents);
            }
            else
            {
                Assert.Equal(content, Encoding.UTF8.GetString(fileContentResult.FileContents));
            }
        }
コード例 #46
0
        public void ControllerCreatedAtAction_InvokedInUnitTests()
        {
            // Arrange
            var controller = new TestabilityController();
            var actionName = "ActionName_1";
            var controllerName = "ControllerName_1";
            var routeValues = new Dictionary<string, object>() { { "route", "sample" } };
            var value = new { Value = "Value_1" };

            // Act
            var result = controller.CreatedAtAction_Action(actionName, controllerName, routeValues, value);

            // Assert
            Assert.NotNull(result);

            var createdAtActionResult = Assert.IsType<CreatedAtActionResult>(result);
            Assert.Equal(actionName, createdAtActionResult.ActionName);
            Assert.Equal(controllerName, createdAtActionResult.ControllerName);
            Assert.Single(createdAtActionResult.RouteValues);
            Assert.Equal("sample", createdAtActionResult.RouteValues["route"]);
            Assert.Same(value, createdAtActionResult.Value);

            // Arrange
            controller = new TestabilityController();

            // Act
            result = controller.CreatedAtAction_Action(null, null, null, null);

            // Assert
            Assert.NotNull(result);

            createdAtActionResult = Assert.IsType<CreatedAtActionResult>(result);
            Assert.Null(createdAtActionResult.ActionName);
            Assert.Null(createdAtActionResult.ControllerName);
            Assert.Null(createdAtActionResult.Value);
            Assert.Empty(createdAtActionResult.RouteValues);
        }
コード例 #47
0
        public void ControllerActionContext_ReturnsNotNull()
        {
            // Arrange && Act
            var controller = new TestabilityController();

            // Assert
            Assert.NotNull(controller.ActionContext);
            Assert.NotNull(controller.ActionContext.ModelState);
            Assert.Null(controller.ActionContext.ActionDescriptor);
            Assert.Null(controller.ActionContext.HttpContext);
            Assert.Null(controller.ActionContext.RouteData);
        }
コード例 #48
0
        public void ViewComponent_WithName()
        {
            // Arrange
            var controller = new TestabilityController();

            // Act
            var result = controller.ViewComponent("TagCloud");

            // Assert
            Assert.NotNull(result);
            
            Assert.Equal("TagCloud", result.ViewComponentName);
        }