Watch2–part7–full test
Watch2 NuGet Package
The Watch2 NuGet package extends the functionality of dotnet watch
by adding features such as console clearing and delay handling. It outputs the same information as dotnet watch
.
1. Wrapper Interfaces
To facilitate testing, it is necessary to wrap various process creation classes from .NET (e.g., Process
=> ProcessWrapper
, ProcessStartInfo
=> ProcessStartInfoWrapper
). This allows for the testing of process outputs. These wrappers are primarily interfaces that can be mocked and redirected to the main class.
Most of these wrappers can be generated automatically using a Roslyn Code Generator.
Rule of Thumb
If the main class you want to wrap contains properties, methods, or events that return or accept instances of another class, you should create a wrapper for that class as well.
Example 1
The Process
class has a property StartInfo
of type ProcessStartInfo
. Therefore, a wrapper for ProcessStartInfo
must be created.
Example 2
The Process
class has an event OutputDataReceived
of type DataReceivedEventArgs
. Consequently, a wrapper for DataReceivedEventArgs
is required.
2. Rocks and Waiting for an Async Call
When mocking an async method with Rocks, it is essential to wait for the async call to complete.
For example, you must wait for .WaitForExitAsync()
to finish before checking the process output.
Leave a Reply