SparkyTestHelpers.AspNetMvc 1.3.1

.NET Unit test helpers for ASP.NET MVC routing and for asserting that ASP.NET MVC controller actions return expected results

Install-Package SparkyTestHelpers.AspNetMvc -Version 1.3.1
dotnet add package SparkyTestHelpers.AspNetMvc --version 1.3.1
<PackageReference Include="SparkyTestHelpers.AspNetMvc" Version="1.3.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SparkyTestHelpers.AspNetMvc --version 1.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

see also:


ControllerTester<TController>

Instantiation:

using SparkyTestHelpers.AspNetMvc;
    var homeController = new HomeController(/* with test dependencies */);
    var controllerTester = new ControllerTester<HomeController>(homeController);

It doesn't do anything on its own - just provides an Action(actionDefinitionExpression) method that's used to create a...

ControllerActionTester

    var controllerActionTester = 
        new ControllerTester<HomeController>(homeController).Action(x => x.Index);

The .Action method argument is an expression for either a synchronous or async controller action method.

ControllerActionTester has several .Test... methods used to assert that the controller action returns the expected ActionResult implementation. There are methods for all of the standard result types, plus the generic TestResult<TActionResultType> method:

  • .TestContent(Action<ContentResult> validate)
  • .TestEmpty(Action<EmptyResult> validate)
  • .TestFile(Action<FileResult> validate)
  • .TestJson(Action<JsonResult> validate)
  • .TestPartialView(Action<PartialViewResult> validate)
  • .TestRedirect(string expectedUrl, Action<RedirectResult> validate)
  • .TestRedirectToAction(string expecteActionName, Action<RedirectToRouteResult> validate)
  • .TestRedirectToRoute(string expectedRoute, Action<RedirectToRouteResult> validate)
  • .TestView(Action<ViewResult> validate)
  • .TestResult<TActionResultType>(Action<TActionResultType> validate)

Additional methods:

  • .ExpectingViewName(string expectedViewName) - used with .TestView and .TestPartialView
  • .ExpectingModel<TModelType>(Action<TModelType> validate) - using with .TestView and .TestJson
  • WhenModelStateIsValidEquals(bool isValid) - used to test conditional logic based on ModelState.IsValid

All validate "callback" actions shown above are optional.

Examples

    var homeController = new HomeController(/* with test dependencies */);
    var controllerTester = new ControllerTester<HomeController>(homeController);

    controllerTester.Action(x => x.Index).TestView();

    controllerTester
        .Action(x => () => x.Details(3))
        .ExpectingViewName("Details")
        .ExpectingModel<Foo>(foo => Assert.IsTrue(foo.IsValid))
        .TestView();

    controllerTester
        .Action(x => () => x.Edit(updateModel))
        .WhenModelStateIsValidEquals(false)
        .TestRedirectToAction("Errors");

    controllerTester
        .Action(x => () => x.Edit(updateModel))
        .WhenModelStateIsValidEquals(true)
        .ExpectingViewName("UpdateSuccessful")
        .TestRedirectToRoute("Home/UpdateSuccessful");

RouteTester

RouteTester and RoutingAsserter provide methods to assert that a given relative URL maps to the expected RouteData.Values. The RoutingAsserter.AssertMapTo overloads provide multiple ways to specify the expected values...

Constructors

  • public RouteTester(Action<RouteCollection> routeRegistrationMethod)
  • public RouteTester(AreaRegistration areaRegistration)
using SparkyTestHelpers.AspNetMvc.Routing;
. . .
    var routeTester = new RouteTester(RouteConfig.RegisterRoutes);
    var areaRouteTester = new RouteTester(new FooAreaRegistration());

methods

  • .ForUrl(string relativeUrl) - creates a new RoutingAsserter instance.

RoutingAsserter

methods

  • .AssertMapTo(IDictionary<string, object> expectedValues)
  • .AssertMapTo(object routeValues)
  • .AssertMapTo(string controller, string action, (object id)) - id defaults to null
  • .AssertMapTo<TController>(Expression<Func<TController, Func<ActionResult>>> actionExpression)
  • .AssertRedirectTo(string expectedUrl, (HttpStatusCode *expectedStatusCode)) - expectedStatusCode defaults to HttpStatusCode.Redirect (302)

examples

routeTester.ForUrl("Default.aspx)
    .AssertRedirectTo("Home/LegacyRedirect");

// alternate syntaxes for asserting Home/Index routing:
routeTester.ForUrl("Home/Index")
    .AssertMapTo(new Dictionary<string, object> 
        { { "controller", "Home" }, { "action", "Index" }, { "id", null } );
routeTester.ForUrl("Home/Index")
    .AssertMapTo(new {controller = "Home", action = "Index"});
routeTester.ForUrl("Home/Index")
    .AssertMapTo("Home", "Index");
routeTester.ForUrl("Home/Index")
    .AssertMapTo<HomeController>(x => x.Index);

// alternate syntaxes for asserting Order/Details/3 routing:
routeTester.ForUrl("Order/Details/3")
    .AssertMapTo(new Dictionary<string, object> 
        { { "controller", "Order" }, { "action", "Details" }, { "id", 3 } );
routeTester.ForUrl("Order/Details/3")
    .AssertMapTo(new {controller = "Order", action = "Details", id = 3 });
routeTester.ForUrl("Order/Details/3")
    .AssertMapTo("Order", "Details", 3);
routeTester.ForUrl("Order/Details/3")
    .AssertMapTo<OrderController>(x => () => x.Details(3));

see also:


ControllerTester<TController>

Instantiation:

using SparkyTestHelpers.AspNetMvc;
    var homeController = new HomeController(/* with test dependencies */);
    var controllerTester = new ControllerTester<HomeController>(homeController);

It doesn't do anything on its own - just provides an Action(actionDefinitionExpression) method that's used to create a...

ControllerActionTester

    var controllerActionTester = 
        new ControllerTester<HomeController>(homeController).Action(x => x.Index);

The .Action method argument is an expression for either a synchronous or async controller action method.

ControllerActionTester has several .Test... methods used to assert that the controller action returns the expected ActionResult implementation. There are methods for all of the standard result types, plus the generic TestResult<TActionResultType> method:

  • .TestContent(Action<ContentResult> validate)
  • .TestEmpty(Action<EmptyResult> validate)
  • .TestFile(Action<FileResult> validate)
  • .TestJson(Action<JsonResult> validate)
  • .TestPartialView(Action<PartialViewResult> validate)
  • .TestRedirect(string expectedUrl, Action<RedirectResult> validate)
  • .TestRedirectToAction(string expecteActionName, Action<RedirectToRouteResult> validate)
  • .TestRedirectToRoute(string expectedRoute, Action<RedirectToRouteResult> validate)
  • .TestView(Action<ViewResult> validate)
  • .TestResult<TActionResultType>(Action<TActionResultType> validate)

Additional methods:

  • .ExpectingViewName(string expectedViewName) - used with .TestView and .TestPartialView
  • .ExpectingModel<TModelType>(Action<TModelType> validate) - using with .TestView and .TestJson
  • WhenModelStateIsValidEquals(bool isValid) - used to test conditional logic based on ModelState.IsValid

All validate "callback" actions shown above are optional.

Examples

    var homeController = new HomeController(/* with test dependencies */);
    var controllerTester = new ControllerTester<HomeController>(homeController);

    controllerTester.Action(x => x.Index).TestView();

    controllerTester
        .Action(x => () => x.Details(3))
        .ExpectingViewName("Details")
        .ExpectingModel<Foo>(foo => Assert.IsTrue(foo.IsValid))
        .TestView();

    controllerTester
        .Action(x => () => x.Edit(updateModel))
        .WhenModelStateIsValidEquals(false)
        .TestRedirectToAction("Errors");

    controllerTester
        .Action(x => () => x.Edit(updateModel))
        .WhenModelStateIsValidEquals(true)
        .ExpectingViewName("UpdateSuccessful")
        .TestRedirectToRoute("Home/UpdateSuccessful");

RouteTester

RouteTester and RoutingAsserter provide methods to assert that a given relative URL maps to the expected RouteData.Values. The RoutingAsserter.AssertMapTo overloads provide multiple ways to specify the expected values...

Constructors

  • public RouteTester(Action<RouteCollection> routeRegistrationMethod)
  • public RouteTester(AreaRegistration areaRegistration)
using SparkyTestHelpers.AspNetMvc.Routing;
. . .
    var routeTester = new RouteTester(RouteConfig.RegisterRoutes);
    var areaRouteTester = new RouteTester(new FooAreaRegistration());

methods

  • .ForUrl(string relativeUrl) - creates a new RoutingAsserter instance.

RoutingAsserter

methods

  • .AssertMapTo(IDictionary<string, object> expectedValues)
  • .AssertMapTo(object routeValues)
  • .AssertMapTo(string controller, string action, (object id)) - id defaults to null
  • .AssertMapTo<TController>(Expression<Func<TController, Func<ActionResult>>> actionExpression)
  • .AssertRedirectTo(string expectedUrl, (HttpStatusCode *expectedStatusCode)) - expectedStatusCode defaults to HttpStatusCode.Redirect (302)

examples

routeTester.ForUrl("Default.aspx)
    .AssertRedirectTo("Home/LegacyRedirect");

// alternate syntaxes for asserting Home/Index routing:
routeTester.ForUrl("Home/Index")
    .AssertMapTo(new Dictionary<string, object> 
        { { "controller", "Home" }, { "action", "Index" }, { "id", null } );
routeTester.ForUrl("Home/Index")
    .AssertMapTo(new {controller = "Home", action = "Index"});
routeTester.ForUrl("Home/Index")
    .AssertMapTo("Home", "Index");
routeTester.ForUrl("Home/Index")
    .AssertMapTo<HomeController>(x => x.Index);

// alternate syntaxes for asserting Order/Details/3 routing:
routeTester.ForUrl("Order/Details/3")
    .AssertMapTo(new Dictionary<string, object> 
        { { "controller", "Order" }, { "action", "Details" }, { "id", 3 } );
routeTester.ForUrl("Order/Details/3")
    .AssertMapTo(new {controller = "Order", action = "Details", id = 3 });
routeTester.ForUrl("Order/Details/3")
    .AssertMapTo("Order", "Details", 3);
routeTester.ForUrl("Order/Details/3")
    .AssertMapTo<OrderController>(x => () => x.Details(3));

Release Notes

v1.3.0: added async action support

Version History

Version Downloads Last updated
1.3.1 25 5/18/2019
1.3.0 321 3/31/2018
1.2.0 172 3/28/2018
1.1.0 152 3/26/2018
1.0.1 174 3/22/2018
1.0.0 175 3/22/2018