Generic.DummyGenerator.Tool
1.0.2
dotnet tool install --global Generic.DummyGenerator.Tool --version 1.0.2
dotnet new tool-manifest
dotnet tool install --local Generic.DummyGenerator.Tool --version 1.0.2
#tool dotnet:?package=Generic.DummyGenerator.Tool&version=1.0.2
nuke :add-package Generic.DummyGenerator.Tool --version 1.0.2
Dummy Generator
In integration tests, the data objects that need to be provided to the test method are often complex and nested. According to IOSP, we separate data from integration and operation and have the following requirements for our data classes:
- Data contains only data, no methods
- Data objects are immutable
- Data objects are fully initialized after the constructor call
In C#, we now find these requirements implemented as a language feature with the record concept: The primary constructor forces us to set all parameters, which helps us meet our requirements, and we do not deviate from this.
In tests, however, we must always call the constructor, both for incoming parameters and for the expected object, in every test, even if some values do not interest us. When data objects change, all places in tests must be adjusted: production efficiency decreases.
The Dummy Generator provides a remedy by following the principle of dummy factory methods. This allows for the effortless creation of always suitable test objects that do not break when data structures change. The Dummy Generator performs the following:
- There is a (static) class "Dummies".
- In this class, there is a method for each data type, named after the data type for which it should create an object.
- The method contains a default parameter for each property defined by the type (either a meaningful primitive value or null). Only direct properties can be controlled via parameters.
- Parameters that are potentially set to null are initialized in the implementation (using other dummy methods if necessary).
The default values should be plausible for the "happy path" but do not necessarily have to be internally consistent.
using static Dummies;
...
[Test]
public void QueryShouldMatchExpected()
{
var request = Request();
var actual = ProductionCode.Query(request);
var expected = Response(complexType2: ResponseComplexType2(9422));
actual.Should().BeEquivalentTo(expected);
}
...
Usage
Once the tool is installed you can start it using this command:
dummygenerator
- It will ask for the assembly where your data classes (preferably C# records) are located.
- The namespace will be extracted using the assembly provided, but can be changed in the next step (leave blank if you are fine with the default).
- The dummygenerator crawls the assembly and displays all types by namespaces in a tree view
- If it finds any records, these will be selected by default.
- Use the
arrowkeys⬆️and⬇️to navigate through namespaces and types - Use the
space barto change selection
- Once you are happy with the selection, hit
enter - The dummygenerator will create a dummy class and display its content in the console
- Use the
arrowkeys⬆️and⬇️to select your next command, if needed. You can also copy the code from the console if your OS allows to do so. - Commands are as follows:
LoadAssembly
Starts the process all overSetRecordsAsDefaults
No matter what your previous selection was, all (and only) C# records are selectedChooseTypes
Allows to select types of the loaded assembly (and also generates a new dummy class)SetNamespace
Changes the namespace of the generated dummy class (and also re-generate it)CopyToClipboard
Copy the dummy class content into the clipboard.SaveToFile
Will save the dummy class content into a file by the name provided (defaults to assembly directory + Dummies.cs)Exit
Exits the tool
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
This package has no dependencies.
| Version | Downloads | Last Updated |
|---|
1.0.2 (July 4, 2024)
- Initial public release