UnitTestingHelper 6.0.11
dotnet add package UnitTestingHelper --version 6.0.11
NuGet\Install-Package UnitTestingHelper -Version 6.0.11
<PackageReference Include="UnitTestingHelper" Version="6.0.11" />
paket add UnitTestingHelper --version 6.0.11
#r "nuget: UnitTestingHelper, 6.0.11"
// Install UnitTestingHelper as a Cake Addin #addin nuget:?package=UnitTestingHelper&version=6.0.11 // Install UnitTestingHelper as a Cake Tool #tool nuget:?package=UnitTestingHelper&version=6.0.11
UnitTestingHelper
Simple Nuget package to support unit testing constructs for TDD unit tests. The project contains a single method with two overrides to allow unit tests to be written without any code implementation existing first i.e. stubbed out Classes and Methods to allow the Visual Studio compiler to work and for the unit tests to be run in the Red → Green refactoring pattern for TDD unit tests.
Initially being new to TDD I found the transition awkward at first from the more traditional unit testing approach, and this code helped me get started and appreciate the benefits of TDD testing. So I have released this Nuget package with the aim to help those that might be experiencing difficulties in getting acquainted to TDD.
This Nuget package is NOT designed for the more experienced TDD unit testers, but can be used if this concept is preferred over other TDD approaches to TDD testing principles developing C# test projects in Visual Studio
Target Framework
This package targets the .net 6 framework
Examples
The following example TDD unit test is for a method under test named 'AddTwoNumbersTogetherReturnIntegerSumValue' this method is intended to accept two integer values and return an integer value representing the sum of the two integers passed in.
Neither the Project, Namespace, Class or Method are required to be implemented at this stage, instead the string representation of the Project, Namespace, Class or Method is used, ready for the TDD Red → Green refactoring steps
Example of a Unit Test with Type (No Dependencies)
[TestMethod]
public void TestMethod()
{
var returnValue = Helper.TestMethod<int>(new object[] {},
"Namespace.ClassName, ProjectName",
"AddTwoNumbersTogetherReturnIntegerSumValue", new object[] { 1, 2 });
Assert.IsTrue(returnValue == 3);
}
The example unit test above can be run without any code and will 'Fail' (Red) but the solution / projects will compile successfully in Visual Studio. The developer can now implement the required Type and Method as outlined in the unit test to obtain a 'Success' (Green) unit test status.
Now the developer has a basic working unit test, the process of refactoring can begin to implement the business logic with the support of an existing unit test to support future refactoring stages for this method.
Example below is a Unit Test with a Type under test requiring two dependencies, in this example Dependency Types already exist and are mocked in this unit test
[TestMethod]
public void TestMethod()
{
Mock<IDependency1> _mockDependency1 = new();
Mock<IDependency2> _mockDependency2 = new();
var returnValue = Helper.TestMethod<int>(new object[]
{ _mockDependency1.Object, _mockDependency2.Object },
"Namespace.ClassName, ProjectName",
"AddTwoNumbersTogetherReturnIntegerSumValue", new object[] { 1, 2 });
Assert.IsTrue(returnValue == 3);
}
This unit test requires two known dependencies and as such is an example of how to use this package to define a unit test where existing objects exist for a new method being defined in a TDD unit test.
If the Method under test has no return type, the TestMethod() call has an override that does not require a return type (T) declaration
Generic Type Support
Example unit test below for generic types passed into method calls. Classes with generic types must exist and preferably have there own TDD unit tests defined before being used in method parameters. Below is an example of a unit test and for reference the Interfaces, Classes, Properties and Methods under test.
[TestMethod]
public void TestMethod()
{
var returnValue = Helper.TestMethod<Task<bool>>(Array.Empty<object>(),
"Namespace.ClassName, ProjectName",
"MakeWidgetThings", new object[] { new Widget<string, int, double>(), 1});
Assert.IsTrue(returnValue?.Result == true);
}
public interface IThing
{
Task<bool> MakeWidgetThings<T, R, D>(Widget<T, R, D> widget, int quantity);
}
public class Thing : IThing
{
public Task<bool> MakeWidgetThings<T, R, D>(Widget<T, R, D> widget, int quantity = 1)
{
return Task.FromResult(true);
}
}
public class Widget<T, R, D>
{
public T? Thing1 { get; set; }
public R? Thing2 { get; set; }
public D? Thing3 { get; set; }
}
The example Widget class above has three Generic types for the properties. Class Thing has a the method MakeWidgetThings that has two parameters Widget type and Integer and the three generics required by the Widget class.
The unit test defines these method parameters as follows : new object[] { new Widget<string, int, double>(), 1 } this signature is similar to normal calls for Helper.TestMethod(), the package examines the parameters to check for generics and handles the creation of the instances for TDD unit testing.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Fix for case when target method Invoke fails for undefined return type, added exception message call TestMethod<T>() if a return type specified