TestBase 3.0.2

*TestBase* gets you off to a flying start when unit testing projects with dependencies.
It offers a rich extensible set of fluent assertions and a set of verifiable Fake Ado.Net components, with easy setup and verification.

TestBase.Shoulds
------------------
Chainable fluent assertions drill down to the point concisely.
UnitUnderTest.Action().ShouldNotBeNullOrEmpty().ShouldContain(x=>x.ExpectedField == expected);
* Lots of Shoulds: ShouldBe(),ShouldSatisfy(),ShouldHave(),ShouldBeTrue(),ShouldBeGreaterThan(),ShouldBeLessThan(),ShouldBeGreaterThanOrEqual(),ShouldBeAssignableTo(),ShouldBeBetween(),ShouldBeNullOrEmpty(),...
* Enumerable Assertions: ShouldBeEmpty(),ShouldContain(),ShouldContainEachItemOf(),ShouldBeIn(),ShouldHave(),ShouldAll(),...
* Lots of Nots: ShouldNotBe(),ShouldNotBeNullOrEmpty(),ShouldNotContain(),...
* String & Regex Assertions:ShouldContain(),ShouldEqualIgnoringCase(),ShouldMatch(),ShouldNotBeNullOrEmptyOrWhitespace(),...
* StreamAssertions:ShouldContain(stream),ShouldContain(byte[]),ShouldEqualByValue(stream)
* ShouldEqualByValue() compares all kinds of objects and collections field-by-field, by value, basis
new{f1=1,f2="2"}ShouldEqualByValue(new{f1=1,f2="2"});
new[]{1,2}.ShouldEqualByValue(new List<int>{1,2});

TestBase.FakeDb
------------------
Works with Ado.Net and technologies on top of it, including Dapper.
* fakeDbConnection.SetupForQuery(fakeData, new[] {"FieldName1", FieldName2"})
* fakeDbConnection.SetupForExecuteNonQuery(rowsAffected)
* fakeDbConnection.Verify(x=>x.CommandText.Matches("Insert MyTable") && x.Parameters["id"].Value==1)

TestBase.TestBase<T> AutoMocksAndFakes
--------------------------------
is in development and currently works non-recursively. It auto-constructs the UnitUnderTest.
It identifies constructor dependencies by name and type, looking in the following places:
1) Fields in the TestFixture class
2) Entries in the Mocks[] or Fake[] dictionaries
3) Finally it creates a Mock (if it's a mockable type) or a default instance (if its sealed, or value type) for anything that's missing

See also: TestBase-Mvc for Asp.Net MVC assertions such as ControllerUnderTest.Action().ShouldBeViewResult().ShouldHaveModelOfType<T>().Field1.ShouldEqual(expected);

ChangeLog
---------
3.0.2.0 Added ShouldBeLessThan,ShouldBeIn
2.0.5.0 Adds some intellisense and FakeDbConnection.Verify(..., message,args) overload

There is a newer version of this package available.
See the version list below for details.
Install-Package TestBase -Version 3.0.2
dotnet add package TestBase --version 3.0.2
<PackageReference Include="TestBase" Version="3.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TestBase --version 3.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TestBase, 3.0.2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install TestBase as a Cake Addin
#addin nuget:?package=TestBase&version=3.0.2

// Install TestBase as a Cake Tool
#tool nuget:?package=TestBase&version=3.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Release Notes

Version 1. Unpolished but in use for over a year

NuGet packages (4)

Showing the top 4 NuGet packages that depend on TestBase:

Package Downloads
TestBase-Mvc
*TestBase* gets you off to a flying start when unit testing projects with dependencies. TestBase-Mvc adds a rich extensible set of fluent assertions for verifying Mvc ActionResults and for easy setup of ControllerContext and HttpContext for both Mvc and WebApi TestBase.Shoulds ------------------- Chainable fluent assertions get you to the point concisely ControllerUnderTest.Action() .ShouldbeViewResult() .ShouldHaveModel<TModel>() .ShouldEqualByValue(expected) ControllerUnderTest.Action() .ShouldBeRedirectToRouteResult() .ShouldHaveRouteValue("expectedKey", [Optional] "expectedValue"); ShouldHaveViewDataContaining(), ShouldBeJsonResult() etc. TestBase ---------- Controller extensions to fake the http request & context. By injecting the RegisterRoutes method of your MvcApplication, you can use and test Controller.Url with your application's configured routes. ControllerUnderTest .WithHttpContextAndRoutes( [Optional] Action<RouteCollection> mvcApplicationRoutesRegistration, [optional] string requestUrl, [Optional] string query = "", [Optional] string appVirtualPath = "/", [Optional] HttpApplication applicationInstance) ApiControllerUnderTest.WithWebApiHttpContext<T>( HttpMethod httpMethod, [Optional] string requestUri, [Optional] string routeTemplate)
TestBase.AdoNet
TestBase.AdoNet TestBase.FakeDb ------------------ Fake and verify AdoNet queries and commands ``` - fakeDbConnection.SetupForQuery(IEnumerable<TFakeData>; ) - fakeDbConnection.SetupForQuery(IEnumerable<Tuple<TFakeDataForTable1,TFakeDataForTable2>> ) - fakeDbConnection.SetupForQuery(fakeData, new[] {"FieldName1", FieldName2"}) - fakeDbConnection.SetupForExecuteNonQuery(rowsAffected) - fakeDbConnection.ShouldHaveUpdated("tableName", [Optional] fieldList, whereClauseField) - fakeDbConnection.ShouldHaveSelected("tableName", [Optional] fieldList, whereClauseField) - fakeDbConnection.ShouldHaveUpdated("tableName", [Optional] fieldList, whereClauseField) - fakeDbConnection.ShouldHaveDeleted("tableName", whereClauseField) - fakeDbConnection.ShouldHaveInvoked(cmd => predicate(cmd)) - fakeDbConnection.ShouldHaveExecutedStoredProcedure("name") - fakeDbConnection.ShouldHaveXXX().ShouldHaveParameter("name", value) - fakeDbConnection.Verify(x=>x.CommandText.Matches("Insert [case] .*") && x.Parameters["id"].Value==1) ``` TestBase.RecordingDb -------------------- * `new RecordingDbConnection(IDbConnection)` helps you profile Ado.Net Db calls See also - TestBase - TestBase.Mvc - TestBase.AdoNet - Serilog.Sinks.ListOfString - Extensions.Logging.ListOfString
TestBase.Mvc.AspNetCore
*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
TestBase.AspNetCore.Mvc
*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

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
4.1.4.4 906 7/11/2020
4.1.4.3 2,555 11/20/2018
4.1.4.1 1,257 11/16/2018
4.1.3.2 1,114 10/28/2018
4.1.3.1 1,086 10/26/2018
4.1.2.7 1,123 10/24/2018
4.1.2.4 1,135 10/16/2018
4.1.2.3 2,072 5/22/2018
4.1.2.2 1,213 5/22/2018
4.1.2.1 1,271 5/22/2018
4.1.2 1,346 5/19/2018
4.1.1 1,277 4/8/2018
4.1.0 1,240 4/3/2018
4.0.9.2 1,405 3/31/2018
4.0.9.1 1,921 3/28/2018
4.0.9 1,482 3/23/2018
4.0.8 1,372 3/23/2018
4.0.7 1,285 3/22/2018
4.0.6.2 1,327 3/9/2018
4.0.6.1 1,285 3/7/2018
4.0.5.2 1,335 3/2/2018
4.0.5 1,273 3/1/2018
4.0.4.2 1,267 3/1/2018
4.0.4 1,258 2/25/2018
4.0.3 1,290 2/25/2018
4.0.2 1,204 2/24/2018
4.0.1 1,303 2/24/2018
3.1.0 1,633 7/24/2016
3.0.8.5 1,361 7/23/2016
3.0.8.3 1,402 4/14/2016
3.0.8.2 1,335 3/31/2016
3.0.8.1 1,342 3/30/2016
3.0.8 1,369 3/29/2016
3.0.7.6 1,363 3/14/2016
3.0.7.5 1,337 3/10/2016
3.0.7.4 1,376 2/11/2016
3.0.7.3 1,360 1/29/2016
3.0.6.2 1,443 1/27/2016
3.0.6.1 1,385 1/26/2016
3.0.5 1,410 1/15/2016
3.0.4 1,390 1/13/2016
3.0.3 1,363 12/28/2015
3.0.2 3,274 12/27/2013
3.0.1.1 1,418 12/23/2013
3.0.1 1,475 11/28/2013
2.0.5 1,454 11/28/2013
2.0.4.1 1,446 11/12/2013
2.0.4 1,465 11/12/2013
2.0.3.1 1,421 11/8/2013
2.0.3 1,428 11/7/2013
2.0.2 1,403 11/7/2013
2.0.1 1,466 11/1/2013
1.0.4 1,395 10/31/2013
1.0.3 1,431 10/23/2013
Show less