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
<PackageReference Include="TestBase.AspNetCore.Mvc" Version="4.1.4.3" />
<PackageVersion Include="TestBase.AspNetCore.Mvc" Version="4.1.4.3" />
<PackageReference Include="TestBase.AspNetCore.Mvc" />
paket add TestBase.AspNetCore.Mvc --version 4.1.4.3
#r "nuget: TestBase.AspNetCore.Mvc, 4.1.4.3"
#:package TestBase.AspNetCore.Mvc@4.1.4.3
#addin nuget:?package=TestBase.AspNetCore.Mvc&version=4.1.4.3
#tool nuget:?package=TestBase.AspNetCore.Mvc&version=4.1.4.3
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 | Versions 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. |
-
.NETStandard 2.0
- Microsoft.AspNetCore (>= 2.0.1)
- Microsoft.AspNetCore.Mvc (>= 2.0.2)
- Microsoft.AspNetCore.TestHost (>= 2.0.1)
- Moq (>= 4.8.2)
- TestBase (>= 4.4.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
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