SparkyTestHelpers.AspNetMvc 1.6.8

.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.6.8
dotnet add package SparkyTestHelpers.AspNetMvc --version 1.6.8
<PackageReference Include="SparkyTestHelpers.AspNetMvc" Version="1.6.8" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SparkyTestHelpers.AspNetMvc --version 1.6.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SparkyTestHelpers.AspNetMvc, 1.6.8"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install SparkyTestHelpers.AspNetMvc as a Cake Addin
#addin nuget:?package=SparkyTestHelpers.AspNetMvc&version=1.6.8

// Install SparkyTestHelpers.AspNetMvc as a Cake Tool
#tool nuget:?package=SparkyTestHelpers.AspNetMvc&version=1.6.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

see also:


Controller Testers

ControllerTester<T> and ApiControllerTester<T> test action methods of controllers that inherit from System.Web.Mvc.Controller and System.Web.Http.ApiController respectively.

The general syntax is:

tester<br/>
    .Action(action selection expression)<br/>
    .When(optional code to "arrange" test conditions)<br/>
    .Expecting...(optional code to set up assert expectations)<br/>
    .Test...()

Examples

//ControllerTester:

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

    moviesControllerTester
        .Action(x => () => x.Details(3))
        .ExpectingViewName("Details")
        .ExpectingModel<Movle>(movie => Assert.AreEqual("Office Space", movie.Title))
        .TestView();

    moviesControllerTester
        .Action(x => x.Edit(testInvalidModel))
        .WhenModelStateIsValidEquals(false)
        .TestRedirectToAction("Errors");

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

//ApiControllerTester:

   moviesApiControllerTester 
        .Action<IEnumerable<Movie>>(x => x.GetAllMovies)
        .Test(movies => Assert.AreEqual(100, movies.Count());

   moviesApiControllerTester 
        .Action(x => () => x.Get)
        .WhenRequestHasQueryStringParameters(new QueryStringParameter("id", 3))
        .TestOkNegotiatedContentResult<Movie>(movie => Assert.AreEqual("Office Space", movie.Title));

    moviesApiControllerTester
        .Action(x => () => x.Update(updateModel))
        .WhenModelStateIsValidEquals(false)
        .TestBadRequestResult();

    moviesApiControllerTester
        .Action(x => () => x.Update(updateModel))
        .WhenModelStateIsValidEquals(true)
        .TestOkResult();

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));

Complete API documentation:

https://github.com/BrianSchroer/sparky-test-helpers/blob/master/SparkyTestHelpers.AspNetMvc/api.md

see also:


Controller Testers

ControllerTester<T> and ApiControllerTester<T> test action methods of controllers that inherit from System.Web.Mvc.Controller and System.Web.Http.ApiController respectively.

The general syntax is:

tester<br/>
    .Action(action selection expression)<br/>
    .When(optional code to "arrange" test conditions)<br/>
    .Expecting...(optional code to set up assert expectations)<br/>
    .Test...()

Examples

//ControllerTester:

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

    moviesControllerTester
        .Action(x => () => x.Details(3))
        .ExpectingViewName("Details")
        .ExpectingModel<Movle>(movie => Assert.AreEqual("Office Space", movie.Title))
        .TestView();

    moviesControllerTester
        .Action(x => x.Edit(testInvalidModel))
        .WhenModelStateIsValidEquals(false)
        .TestRedirectToAction("Errors");

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

//ApiControllerTester:

   moviesApiControllerTester 
        .Action<IEnumerable<Movie>>(x => x.GetAllMovies)
        .Test(movies => Assert.AreEqual(100, movies.Count());

   moviesApiControllerTester 
        .Action(x => () => x.Get)
        .WhenRequestHasQueryStringParameters(new QueryStringParameter("id", 3))
        .TestOkNegotiatedContentResult<Movie>(movie => Assert.AreEqual("Office Space", movie.Title));

    moviesApiControllerTester
        .Action(x => () => x.Update(updateModel))
        .WhenModelStateIsValidEquals(false)
        .TestBadRequestResult();

    moviesApiControllerTester
        .Action(x => () => x.Update(updateModel))
        .WhenModelStateIsValidEquals(true)
        .TestOkResult();

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));

Complete API documentation:

https://github.com/BrianSchroer/sparky-test-helpers/blob/master/SparkyTestHelpers.AspNetMvc/api.md

Release Notes

v1.6.0: added support for testing actions that return any response type

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.6.8 72 3/5/2021
1.6.7 31 3/5/2021
1.6.6 53 2/28/2021
1.6.4 39 2/24/2021
1.6.3 39 2/23/2021
1.6.2 72 2/23/2021
1.6.1 34 2/22/2021
1.6.0 32 2/22/2021
1.5.0 38 2/20/2021
1.4.0 104 1/7/2021
1.3.1 988 5/18/2019
1.3.0 616 3/31/2018
1.2.0 497 3/28/2018
1.1.0 447 3/26/2018
1.0.1 501 3/22/2018
1.0.0 530 3/22/2018