TestBase.Mvc (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  TestBase.Mvc is renamed to https://www.nuget.org/packages/TestBase.Mvc.AspNetCore  Use https://www.nuget.org/packages/TestBase-Mvc for Mvc 4.  Chainable fluent assertions get you to the point concisely: ``` - ShouldEqualByValue() & ShouldEqualByValueExceptFor()    work with all kinds of object and collections, and    report what differed. - string.ShouldMatch().ShouldEqualIgnoringCase(), ... - numeric.ShouldBeBetween().ShouldEqualWithTolerance(), ... - IEnumerable.ShouldAll().ShouldContain().ShouldNotContain(), ...  ```  TestBase.Mvc.AspNetCore & TestBase-Mvc ------------  ``` ControllerUnderTest.Action()   .ShouldbeViewResult()   .ShouldHaveModel<TModel>()     .Payload     .ShouldEqualByValueExceptFor(new {Id=1, Payload1=expected}, ignoreFields);  ControllerUnderTest.Action()   .ShouldBeRedirectToRouteResult()   .ShouldHaveRouteValue(""expectedKey"", [Optional] ""expectedValue"");  ShouldHaveViewDataContaining(), ShouldBeJsonResult() etc. ``` Quickly test AspNetCore controllers with zero setup using `WithControllerContext()` :  ``` [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.ControllerContext.HttpContext.Request.ShouldNotBeNull();     uut.Request.ShouldNotBeNull();     uut.Response.ShouldNotBeNull();     uut.Url.ShouldNotBeNull();     uut.ViewData.ShouldNotBeNull();     uut.TempData.ShouldNotBeNull();      uut.MyAction(param)         .ShouldBeViewResult()         .ShouldHaveModel<YouSaidViewModel>()         .YouSaid.ShouldBe(param); }  [Test] public void ShouldBeViewWithModel_And_UrlHelper_ShouldWork() {     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 Tests : 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  - TestBase.HttpClient.Fake  - TestBase.AdoNet  - Serilog.Sinks.ListOfString   - Extensions.Logging.ListOfString
Keywords: nunit|test|unittest|tdd|fluent|assertions|mock|mvc|aspnetmvc|aspnetcore|aspnetcoremvc