You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Aaron Hanusa edited this page Feb 1, 2021
·
17 revisions
Peasy was designed from the ground up following the SOLID design principles. As a result, testing your classes that derive from and implement peasy classes and interfaces is easy.
This document provides guidance and examples of how to test your concrete implementations of peasy actors that should be tested. Note that these tests use a handy assertion framework called shouldly.
A word on unit testing styles
There is much debate around which stylistic unit tests are best, and essentially boils down to state-based vs. behavior-based unit tests. There are pros and cons to each approach and these will not be covered in this documentation.
With regards to state-based testing, you can create an in-memory data proxy project and use your concrete in-memory proxies in your unit tests. To save you time, you can use an abstract base class located in the Peasy.DataProxy.InMemory project. This class can be downloaded and added to your project, or by installed via the Nuget package. More details can be found on the project website.
With regards to behavior-based unit testing, there are some really excellent tools in the .NET testing ecosystem, including Moq, RhinoMocks, and FakeItEasy to name a few.
And of course you can always use state-based and behavior-based unit tests in conjunction with each other.
Testing Rules
Given the following business rule ...
publicclassCustomerAgeVerificationRule:RuleBase{privateDateTime_birthDate;publicCustomerAgeVerificationRule(DateTimebirthDate){_birthDate=birthDate;}// Synchronous supportprotectedoverridevoidOnValidate(){if((DateTime.Now.Year-_birthDate.Year)<18){Invalidate("New users must be at least 18 years of age");}}// Asynchronous supportprotectedoverrideasyncTaskOnValidateAsync(){OnValidate();}}
And testing it out...
varrule=newCustomerAgeVerificationRule(DateTime.Now.AddYears(-17));rule.Validate().IsValid.ShouldBe(false);rule.Validate().ErrorMessage.ShouldBe("New users must be at least 18 years of age");varrule=newCustomerAgeVerificationRule(DateTime.Now.AddYears(-18));rule.Validate().IsValid.ShouldBe(true);rule.Validate().ErrorMessage.ShouldBe(null);
Testing Commands
In the following example, we cover the implementation of the DeleteOrderCommand, which is responsible for deleting an order and all of its associated order items. A full implementation of the command can be located here.
For brevity sake, we will just cover the execution method of the command, leaving out the business rule coverage.
In this example, the deletion of an order and its associated order items are invoked within a transaction context.
Let's take a look at how we might add code coverage around this functionality. A full implementation of all of the tests for this class can be located here.
In this example, we create a state-based unit test by consuming in-memory data proxy implementations and ensuring that they contain the correct record counts after invoking the command.