-
Notifications
You must be signed in to change notification settings - Fork 69
Testing
Aaron Hanusa edited this page Oct 27, 2015
·
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.
Given the following business rule ...
public class CustomerAgeVerificationRule : RuleBase
{
private DateTime _birthDate;
public CustomerAgeVerificationRule(DateTime birthDate)
{
_birthDate = birthDate;
}
// Synchronous support
protected override void OnValidate()
{
if ((DateTime.Now.Year - _birthDate.Year) < 18)
{
Invalidate("New users must be at least 18 years of age");
}
}
// Asynchronous support
protected override async Task OnValidateAsync()
{
OnValidate();
}
}
And testing it out...
var rule = new CustomerAgeVerificationRule(DateTime.Now.AddYears(-17));
rule.Validate().IsValid.ShouldBe(false);
rule.Validate().ErrorMessage.ShouldBe("New users must be at least 18 years of age");
var rule = new CustomerAgeVerificationRule(DateTime.Now.AddYears(-18));
rule.Validate().IsValid.ShouldBe(true);
rule.Validate().ErrorMessage.ShouldBe(null);
Coming soon!