Unit-Testing Overview

Modddels come with a simplified way to unit-test them.

Let's say you have an Age ValueObject you want to unit-test. First, in the @Modddel annotation, set generateTestClasses to true :

@Modddel(
  validationSteps: [
    ValidationStep(
      [Validation('legal', FailureType<AgeLegalFailure>()),],
    ),
  ],
  generateTestClasses: true,
)

This will generate two classes :

  • TestAge : This is what we call the "Tester". It allows you to create tests.

  • AgeParams : This is what we call the "ModddelParams". It represents the parameters of the modddel.

Now, in your unit-test file, you can start adding some tests this way :

void main() {
  // 1.
  const testAge = TestAge();

  // 2.
  group('Scenario : Age is valid', () {
    
    //3.
    testAge.isValid(const AgeParams(19));

    testAge.isValid(const AgeParams(20));
  });
}

As you can see :

  1. You create an instance of the Tester

  2. Optionally, you can group your tests in group methods

  3. You add your tests. In this example, we created two tests : the first one verifies that the Age is valid when given 19 as an argument, and the second 20.