TestBase.Mvc.AspNetCore (nuget): *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      - 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
Keywords: nunit|test|unittest|tdd|fluent|assertions|mock|mvc|aspnetmvc|aspnetcore|aspnetcoremvc