TestBase – Rich, fluent assertions and tools for testing with heavyweight dependencies: AspNetCore, AdoNet, HttpClient, AspNet.Mvc, Streams, Logging NuGet Package

TestBase.AspNetCore.Mvc
-----------------------
Use Controller.WithControllerContext() extension methods to stub the
HttpRequest and HttpContext, enabling you to unit test IActionResults,
even with dependencies on Controller.Url(), Request.Cookies, Response.Cookies
and more.

Chainable fluent assertions get you to the point concisely:
```
.ShouldBeViewResult()
.ShouldBeViewNamed()
.ShouldBeDefaultView()
.ShouldBePartialViewResult()

.ShouldBeViewWithModel()
.ShouldHaveModel()
.ShouldBeAnInvalidModel()

.ShouldHaveViewDataContaining()
.ShouldHaveViewDataForKey()
.ViewData.ShouldContainKey()

.ShouldBeJsonResult()

.ShouldBeFileResult()
.ShouldBeFileContentResult()
.ShouldBeFileStreamResult()

.ShouldBeRedirectResult()
.ShouldBeRedirectToRouteResult()
.ShouldBeRedirectToDefaultAction()
.ShouldBeRedirectToActionResult()
.ShouldBeRedirectToRouteResultWithController()
.ShouldBeRedirectToDefaultActionAndController()
.ShouldHaveRouteValue()
.ShouldHaveRouteIdValue()
```

```
var controllerUnderTest = new AController().WithControllerContext();

controllerUnderTest
.AView("parameter", "Thing")
.ShouldBeViewWithModel<MyViewModel>("ViewName")
.AProperty.ShouldBe("Thing");

controllerUnderTest
.Url
.Action("AView", "ATest", new {id=99})
.ShouldEqual("/ATest/AView?id=99");

var fileResult=controllerUnderTest
.AFileResult()
.ShouldBeFileContentResult();
fileResult.FileContents
.ShouldEqualByValue( Encoding.UTF8.GetBytes("my words"));

fileResult.ContentType.ShouldBe("text/plain");
fileResult.FileDownloadName.ShouldBe("filename.txt");

// and ShouldHaveViewDataContaining(), ShouldBeJsonResult() etc.
```


See also
- TestBase
- TestBase.HttpClient.Fake
- TestBase.AdoNet
- TestBase.Mvc for NetFramework AspNet Mvc 4 and 5
- Serilog.Sinks.ListOfString
- Extensions.Logging.ListOfString.




Got any TestBase – Rich, fluent assertions and tools for testing with heavyweight dependencies: AspNetCore, AdoNet, HttpClient, AspNet.Mvc, Streams, Logging Question?





Info

Version: 4.1.4.3
Author(s): Chris F Carroll
Last Update: Sunday, March 22, 2026
.NET Fiddle: Create the first Fiddle
Project Url: https://github.com/chrisfcarroll/TestBase
NuGet Url: https://www.nuget.org/packages/TestBase.AspNetCore.Mvc


Install
Install-Package TestBase.AspNetCore.Mvc
dotnet add package TestBase.AspNetCore.Mvc
paket add TestBase.AspNetCore.Mvc
TestBase.AspNetCore.Mvc Download (Unzip the "nupkg" after downloading)



Tags



STATS

must-have-score

.5

avg-downloads-per-day

1

days-since-last-release

180