TestBase.Mvc
4.0.6.1
See the version list below for details.
dotnet add package TestBase.Mvc --version 4.0.6.1
NuGet\Install-Package TestBase.Mvc -Version 4.0.6.1
<PackageReference Include="TestBase.Mvc" Version="4.0.6.1" />
paket add TestBase.Mvc --version 4.0.6.1
#r "nuget: TestBase.Mvc, 4.0.6.1"
// Install TestBase.Mvc as a Cake Addin #addin nuget:?package=TestBase.Mvc&version=4.0.6.1 // Install TestBase.Mvc as a Cake Tool #tool nuget:?package=TestBase.Mvc&version=4.0.6.1
TestBase gets you off to a flying start when unit testing, especially for projects with dependencies on AspNetMvc, HttpClient or Ado.Net It has rich, yet so easily extensible, fluent assertions, including EqualsByValue, Regex, Stream Comparision, Ado.Net,Mvc and HttpResponseMessage assertions.
Fluent Assertions
Chainable fluent assertions get you to the point concisely
* ShouldEqualByValue(), ShouldEqualByValueExceptFor()
* work with all kinds of object and collections, and pinpoint what was different.
* ShouldBe(), ShouldNotBe(), ShouldBeOfType(), ...
* string shoulds: ShouldMatch() ShouldNotBeNullOrEmptyOrWhiteSpace(),
ShouldEqualIgnoringCase(), ShouldBeContainedIn(), ...
* numeric shoulds: ShouldBeBetween(), ShouldEqualWithTolerance(),
GreaterThan, LessThan, GreaterOrEqualTo ...
* IEnumerable shoulds: ShouldAll(), ShouldContain(), ShouldNotContain(),
ShouldBeEmpty(), ShouldNotBeEmpty() and more
* Stream shoulds: ShouldHaveSameStreamContentAs() , Stream.ShouldContain()
TestBase.Mvc
ControllerUnderTest.Action()
.ShouldbeViewResult()
.ShouldHaveModel<TModel>()
.ShouldEqualByValue(expected)
ControllerUnderTest.Action()
.ShouldBeRedirectToRouteResult()
.ShouldHaveRouteValue(""expectedKey"", [Optional] ""expectedValue"");
UnitUnderTest.Action()
.ShouldNotBeNull()
.ShouldEqualByValue(new {Id=1, Payload=expected, Additional=new[]{ expected1, expected2 }} )
.Payload
.ShouldMatchIgnoringCase(""I expected this"");
ShouldHaveViewDataContaining(), ShouldBeJsonResult() etc.
TestBase.Mvc Version 4 for netstandard20 & AspNetCore Mvc
- Test most controllers with zero setup using
controllerUnderTest.WithControllerContext(actionUnderTest)
:
[Test]
public void ShouldBeViewWithModel_ShouldAssertViewResultAndNameAndModel()
{
var controllerUnderTest = new AController().WithControllerContext("Action");
var result= controllerUnderTest.ActionName().ShouldBeViewWithModel<AClass>("ViewName");
result.ShouldBeOfType<AClass>().FooterLink.ShouldBe("/AController/ActionName");
}
- Test controllers with complex application dependencies using
HostedMvcTestFixtureBase
and specify your MVCApplicationsStartup
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 );
}
}
TestBase.Mvc Version 3 for Net4
Use the Controller.WithHttpContextAndRoutes()
extension methods 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.FakeDb
Works with Ado.Net and technologies on top of it, including Dapper.
* 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.ShouldHaveXXX().ShouldHaveParameter(""name"", value)
* fakeDbConnection.Verify(x=>x.CommandText.Matches(""Insert [case] .*"") && x.Parameters[""id""].Value==1)
Can be used in both NUnit & MS UnitTestFramework test projects.
Testable Logging with StringListLogger
:
MS Logging: ILoggerFactory factory=new LoggerFactory.AddProvider(new StringListLoggerProvider())
Serilogging: new LoggerConfiguration().WriteTo.StringList(stringList).CreateLogger()
//
var logger= factory.CreateLogger("Test1") ; ... ; StringListLogger.Instance.LoggedLines.ShouldContain(x=>x.Matches("kilroy was here")
- Building on Mono : define compile symbol NoMSTest to remove dependency on Microsoft.VisualStudio.QualityTools.UnitTestFramework
ChangeLog
4.0.6.1 TestBase.Mvc can run controller actions on aspnetcore using controller.WithControllerContext() 4.0.5.2 TestBase.Mvc partially ported to AspNetcore 4.0.4.0 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
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. |
.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.0.6.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.