Info
Version: | 4.1.2.2 |
Author(s): | Chris F Carroll |
Last Update: | Saturday, May 26, 2018 |
.NET Fiddle: | Create the first Fiddle |
Project Url: | http://github.com/chrisfcarroll/TestBase |
NuGet Url: | https://www.nuget.org/packages/TestBase.Mvc.AspNetCore |
Install
Install-Package TestBase.Mvc.AspNetCore
dotnet add package TestBase.Mvc.AspNetCore
paket add TestBase.Mvc.AspNetCore
TestBase.Mvc.AspNetCore Download (Unzip the "nupkg" after downloading)
Dependencies
- TestBase(>= 4.1.2.3)
- Microsoft.AspNetCore(>= 2.0.1)
- Microsoft.AspNetCore.Mvc(>= 2.0.2)
- Microsoft.AspNetCore.TestHost(>= 2.0.1)
- Moq(>= 4.8.2)
Tags
- fluent assertions that are easy to extend
- sharp error messages
- tools to help you test with “heavyweight” dependencies on
- AspNetCore.Mvc, AspNet.Mvc or WebApi Contexts
- HttpClient
- Ado.Net
- Streams & Logging
Chainable fluent assertions get you to the point concisely:
```
ControllerUnderTest.Action()
.ShouldbeViewResult()
.ShouldHaveModel<TModel>()
.ShouldEqualByValue(expected, exceptForTheseFields);
.Reference
.ShouldMatchIgnoringCase("I expected this");
ControllerUnderTest.Action()
.ShouldBeRedirectToRouteResult()
.ShouldHaveRouteValue(""expectedKey"", [Optional] ""expectedValue"");
ShouldHaveViewDataContaining(), ShouldBeJsonResult() etc.
```
Quickly test AspNetCore controllers with zero setup using `controllerUnderTest.WithControllerContext()` :
```
[TestFixture]
public class WhenTestingControllersUsingFakeControllerContext
{
[Test]
public void ControllerUrlAndOtherPropertiesShouldWorkAsExpected__GivenControllerContext()
{
var uut = new FakeController().WithControllerContext();
uut.Url.Action(""a"", ""b"").ShouldEqual(""/b/a"");
uut.ControllerContext.ShouldNotBeNull();
uut.HttpContext.ShouldBe(uut.ControllerContext.HttpContext);
uut.Request.ShouldNotBeNull();
uut.ViewData.ShouldNotBeNull();
uut.TempData.ShouldNotBeNull();
uut.MyAction(param)
.ShouldBeViewResult()
.ShouldHaveModel<YouSaidViewModel>()
.YouSaid.ShouldBe(param);
}
[Test]
public void ShouldBeAbleToUseServicesConfiguredInStartupInTests()
{
var moreServicesFromDI=TestServerBuilder.RunningServerUsingStartup<TStartup>().Host.ServiceProvider;
var controllerUnderTest =
new AController()
.WithControllerContext(virtualPathTemplate:""/{Action}/Before/{Controller}"");
var result= controllerUnderTest
.Action(""SomeController"",""SomeAction"")
.ShouldBeViewWithModel<AClass>(""ViewName"");
.FooterLink
.ShouldBe(""/SomeAction/Before/SomeController"");
}
}
```
..
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 );
}
}
```
See also
- TestBase
- TestBase.Mvc for Mvc4 and Mvc 5
- TestBase.HttpClient.Fake
- TestBase.AdoNet
- Serilog.Sinks.ListOfString
- Extensions.Logging.ListOfString.