TestBase-Mvc
4.1.4.3
*TestBase* gets you off to a flying start when unit testing projects with dependencies.
TestBase-Mvc adds a rich extensible set of fluent assertions for verifying Mvc ActionResults and for easy setup of ControllerContext and HttpContext for both Mvc and WebApi
TestBase.Shoulds
-------------------
Chainable fluent assertions get you to the point concisely
ControllerUnderTest.Action()
.ShouldbeViewResult()
.ShouldHaveModel<TModel>()
.ShouldEqualByValue(expected)
ControllerUnderTest.Action()
.ShouldBeRedirectToRouteResult()
.ShouldHaveRouteValue("expectedKey", [Optional] "expectedValue");
ShouldHaveViewDataContaining(), ShouldBeJsonResult() etc.
TestBase
----------
Controller extensions to fake the http request & context. By injecting the RegisterRoutes method of your
MvcApplication, you can use and test Controller.Url with your application's configured routes.
ControllerUnderTest
.WithHttpContextAndRoutes(
[Optional] Action<RouteCollection> mvcApplicationRoutesRegistration,
[optional] string requestUrl,
[Optional] string query = "",
[Optional] string appVirtualPath = "/",
[Optional] HttpApplication applicationInstance)
ApiControllerUnderTest.WithWebApiHttpContext<T>(
HttpMethod httpMethod,
[Optional] string requestUri,
[Optional] string routeTemplate)
Install-Package TestBase-Mvc -Version 4.1.4.3
dotnet add package TestBase-Mvc --version 4.1.4.3
<PackageReference Include="TestBase-Mvc" Version="4.1.4.3" />
paket add TestBase-Mvc --version 4.1.4.3
#r "nuget: TestBase-Mvc, 4.1.4.3"
TestBase gives you a flying start with
- fluent assertions that are easy to extend
- sharp error messages
- tools to help you test with "heavyweight" dependencies on
- AspNet.Mvc or AspNetCore Contexts
- HttpClient
- Ado.Net
- Streams & Logging
- Mix & match with your favourite test runners and assertions.
TestBase.Mvc for Mvc4 and Mvc5
Use the Controller.WithHttpContextAndRoutes()
extension methods to fake the
http request & context, enabling you to unit test with dependecies on
Controller.Url()
, Request.Cookies
, Response.Cookies
Request.QueryString
and more. By passing in your own MvcApplication
's actual RegisterRoutes()
method , you can verify Controller.Url
with your application's
actual routes.
var controllerUnderTest = new AController().WithHttpContextAndRoutes();
controllerUnderTest
.Action("SomeController","SomeAction",other:1)
.ShouldBeViewWithModel<AClass>("ViewName");
.FooterLink
.ShouldBe("/Controller/Action?other=1");
ControllerUnderTest
.WithHttpContextAndRoutes(
[Optional] Action<RouteCollection> mvcApplicationRoutesRegistration,
[optional] string requestUrl,
[Optional] string query = "",
[Optional] string appVirtualPath = "/",
[Optional] HttpApplication applicationInstance)
ApiControllerUnderTest.WithWebApiHttpContext<T>(
HttpMethod httpMethod,
[Optional] string requestUri,
[Optional] string routeTemplate)
Chainable fluent assertions get you to the point concisely:
UnitUnderTest.Action()
.ShouldNotBeNull()
.ShouldEqualByValueExceptFor(new {Id=1, Descr=expected}, ignoreList )
.Payload
.ShouldMatchIgnoringCase("I expected this")
.Should(someOtherPredicate);
.Items
.ShouldAll(predicate)
.ShouldContain(item)
.ShouldNotContain(predicate)
.Where(predicate)
.SingleOrAssertFail()
.ShouldEqualByValue().ShouldEqualByValueExceptFor(...).ShouldEqualByValueOnMembers()
work with all kinds of object and collections, and report what differed.
string.ShouldMatch(pattern).ShouldNotMatch().ShouldBeEmpty().ShouldNotBeEmpty()
.ShouldNotBeNullOrEmptyOrWhiteSpace().ShouldEqualIgnoringCase()
.ShouldContain().ShouldStartWith().ShouldEndWith().ShouldBeContainedIn().ShouldBeOneOf().ShouldNotBeOneOf()
numeric.ShouldBeBetween().ShouldEqualWithTolerance()....GreaterThan....LessThan...GreaterOrEqualTo ...
ienumerable.ShouldAll().ShouldContain().ShouldNotContain().ShouldBeEmpty().ShouldNotBeEmpty() ...
stream.ShouldHaveSameStreamContentAs().ShouldContain()
value.ShouldBe().ShouldNotBe().ShouldBeOfType().ShouldBeAssignableTo()...
See also
TestBase gives you a flying start with
- fluent assertions that are easy to extend
- sharp error messages
- tools to help you test with "heavyweight" dependencies on
- AspNet.Mvc or AspNetCore Contexts
- HttpClient
- Ado.Net
- Streams & Logging
- Mix & match with your favourite test runners and assertions.
TestBase.Mvc for Mvc4 and Mvc5
Use the Controller.WithHttpContextAndRoutes()
extension methods to fake the
http request & context, enabling you to unit test with dependecies on
Controller.Url()
, Request.Cookies
, Response.Cookies
Request.QueryString
and more. By passing in your own MvcApplication
's actual RegisterRoutes()
method , you can verify Controller.Url
with your application's
actual routes.
var controllerUnderTest = new AController().WithHttpContextAndRoutes();
controllerUnderTest
.Action("SomeController","SomeAction",other:1)
.ShouldBeViewWithModel<AClass>("ViewName");
.FooterLink
.ShouldBe("/Controller/Action?other=1");
ControllerUnderTest
.WithHttpContextAndRoutes(
[Optional] Action<RouteCollection> mvcApplicationRoutesRegistration,
[optional] string requestUrl,
[Optional] string query = "",
[Optional] string appVirtualPath = "/",
[Optional] HttpApplication applicationInstance)
ApiControllerUnderTest.WithWebApiHttpContext<T>(
HttpMethod httpMethod,
[Optional] string requestUri,
[Optional] string routeTemplate)
Chainable fluent assertions get you to the point concisely:
UnitUnderTest.Action()
.ShouldNotBeNull()
.ShouldEqualByValueExceptFor(new {Id=1, Descr=expected}, ignoreList )
.Payload
.ShouldMatchIgnoringCase("I expected this")
.Should(someOtherPredicate);
.Items
.ShouldAll(predicate)
.ShouldContain(item)
.ShouldNotContain(predicate)
.Where(predicate)
.SingleOrAssertFail()
.ShouldEqualByValue().ShouldEqualByValueExceptFor(...).ShouldEqualByValueOnMembers()
work with all kinds of object and collections, and report what differed.
string.ShouldMatch(pattern).ShouldNotMatch().ShouldBeEmpty().ShouldNotBeEmpty()
.ShouldNotBeNullOrEmptyOrWhiteSpace().ShouldEqualIgnoringCase()
.ShouldContain().ShouldStartWith().ShouldEndWith().ShouldBeContainedIn().ShouldBeOneOf().ShouldNotBeOneOf()
numeric.ShouldBeBetween().ShouldEqualWithTolerance()....GreaterThan....LessThan...GreaterOrEqualTo ...
ienumerable.ShouldAll().ShouldContain().ShouldNotContain().ShouldBeEmpty().ShouldNotBeEmpty() ...
stream.ShouldHaveSameStreamContentAs().ShouldContain()
value.ShouldBe().ShouldNotBe().ShouldBeOfType().ShouldBeAssignableTo()...
See also
Release Notes
v4. Removed dependency on NUnit.
Dependencies
-
- Microsoft.AspNet.Mvc (>= 4.0.20710)
- Microsoft.AspNet.Razor (>= 2.0.30506)
- Microsoft.AspNet.WebApi.Client (>= 4.0.30506)
- Microsoft.AspNet.WebApi.Core (>= 4.0.20710)
- Microsoft.AspNet.WebPages (>= 2.0.30506)
- Moq (>= 4.0.10827)
- Newtonsoft.Json (>= 7.0.1)
- TestBase (>= 4.1.4.1)
Used By
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 |
---|---|---|
4.1.4.3 | 560 | 11/20/2018 |
4.0.0 | 315 | 11/16/2018 |
3.1.0 | 811 | 7/24/2016 |
3.0.8.5 | 561 | 7/23/2016 |
3.0.8.3 | 583 | 4/14/2016 |
3.0.8.2 | 546 | 3/31/2016 |
3.0.8.1 | 573 | 3/30/2016 |
3.0.8 | 589 | 3/29/2016 |
3.0.7.6 | 541 | 3/14/2016 |
3.0.7.5 | 557 | 3/10/2016 |
3.0.7.4 | 618 | 2/11/2016 |
3.0.7.3 | 579 | 1/29/2016 |
3.0.7.2 | 535 | 1/28/2016 |
3.0.7.1 | 521 | 1/27/2016 |
3.0.7 | 559 | 1/27/2016 |
3.0.6.2 | 534 | 1/27/2016 |
3.0.6.1 | 548 | 1/26/2016 |
3.0.5 | 589 | 1/15/2016 |
3.0.4 | 619 | 1/13/2016 |
3.0.3 | 577 | 12/28/2015 |
3.0.2 | 2,445 | 12/27/2013 |
3.0.1.1 | 656 | 12/23/2013 |
3.0.1 | 684 | 11/28/2013 |
2.0.5 | 662 | 11/28/2013 |
2.0.4.1 | 630 | 11/12/2013 |
2.0.4 | 677 | 11/12/2013 |
2.0.3.1 | 629 | 11/8/2013 |
2.0.3 | 676 | 11/7/2013 |
2.0.2 | 638 | 11/7/2013 |
2.0.1 | 668 | 11/1/2013 |
1.0.4 | 659 | 10/31/2013 |
1.0.3 | 666 | 10/23/2013 |