TestBase.AspNetCore.Mvc 4.1.4.3

dotnet add package TestBase.AspNetCore.Mvc --version 4.1.4.3
                    
NuGet\Install-Package TestBase.AspNetCore.Mvc -Version 4.1.4.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TestBase.AspNetCore.Mvc" Version="4.1.4.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TestBase.AspNetCore.Mvc" Version="4.1.4.3" />
                    
Directory.Packages.props
<PackageReference Include="TestBase.AspNetCore.Mvc" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TestBase.AspNetCore.Mvc --version 4.1.4.3
                    
#r "nuget: TestBase.AspNetCore.Mvc, 4.1.4.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TestBase.AspNetCore.Mvc@4.1.4.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TestBase.AspNetCore.Mvc&version=4.1.4.3
                    
Install as a Cake Addin
#tool nuget:?package=TestBase.AspNetCore.Mvc&version=4.1.4.3
                    
Install as a Cake Tool

TestBase gives you a flying start with

  • fluent assertions that are easy to extend
  • sharp error messages
  • tools to help with “heavyweight” dependencies on
    • AspNetCore.Mvc, AspNet.Mvc or WebApi Contexts
    • HttpClient
    • Ado.Net
    • Streams & Logging

TestBase.AspNetCore.Mvc

Use Controller.WithControllerContext() extension methods to stub the HttpRequest & 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");

Or test against complex application dependencies using HostedMvcTestFixtureBase and specify your Startup class:

[TestFixture]
public class WhenTestingControllersUsingAspNetCoreTestTestServer :
HostedMvcTestFixtureBase
{

[TestCase("/dummy/action?id={id}")]
public async Task Get_Should_ReturnActionResult(string url)
{
  var id=Guid.NewGuid();
  var httpClient=GivenClientForRunningServer<Startup>();
  GivenRequestHeaders(httpClient, "CustomHeader", "HeaderValue1");
  var result= await httpClient.GetAsync(url.Formatz(new {id}));

  result
    .ShouldBe_200Ok()
    .Content.ReadAsStringAsync().Result
    .ShouldBe(""Content"");
}

[TestCase("/dummy")]
public async Task Put_Should_ReturnA(string url)
{
  var something= new Fixture().Create<Something>();
  var jsonBody= new StringContent(
                  something.ToJSon(), 
                  Encoding.UTF8, "application/json");
                  
  var httpClient=GivenClientForRunningServer<Startup>();

  GivenRequestHeaders(httpClient, "CustomHeader", "HeaderValue1");

  var result = await httpClient.PutAsync(url, jsonBody);

  result.ShouldBe_202Accepted();
  DummyController.Putted.ShouldEqualByValue( something );
}

}

Simple, easy-to-extend fluent assertions:

.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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.1.4.3 0 3/22/2026
4.1.2.3 4,344 5/27/2018

ChangeLog
     ---------
     4.1.4.2 TestBase fix typos. TestBase.AspNetCore.Mvc added Request.SetRequestCookies()
     4.1.4.1 TestBase stepped down to netstandard 1.6
     4.1.4.0 TestBase.FakeHttpClient stepped down to netstandard 1.2
     4.1.3.1 Corrected Assertion.ToString() to show BoolWithString detail. Added ShouldEqualByValueOnMembers()
     4.1.2.3 TestBase.Mvc.AspNetCore renamed to TestBase.AspNetCore.Mvc
     4.1.2.0 TestBase.Mvc.AspNetCore provides WithControllerContext() and TestServerBuilder
     4.0.7.0 Added TestBase.FakeHttpClient. Added Should(predicate,...) as synonym of ShouldHave(predicate,...)
     4.0.6.2 TestBase.Mvc can run controller actions on aspnetcore using controller.WithControllerContext()
     4.0.5.2 TestBase.Mvc partially ported to netstandard20 / AspNetCore
     4.0.4.1 StreamShoulds
     4.0.3.0 StringListLogger as MS Logger and as Serilogger
     4.0.1.0 Port to NetCore
     3.0.3.0 Improves FakeDb setup
     3.0.x.0 adds and/or corrects missing Shoulds()
     2.0.5.0 adds some intellisense and FakeDbConnection.Verify(..., message,args) overload