Modddels
HomeGithubPub.dev
v0.2
v0.2
  • Motivation
  • Setup
  • Generalities
    • Modddels Overview
    • Validations & Validation steps
    • Structure of a Modddel
    • Member parameters & Dependency parameters
    • Usage
      • Data equality & toString
      • Pattern matching
      • Reading the fields
      • Reading the failures
      • CopyWith
  • ValueObjects
    • ValueObjects Overview
    • Creating a ValueObject
  • Entities
    • Entities Overview
    • ContentValidation & ContentFailure
    • ValidationSteps in Entities
    • SimpleEntity
      • Creating a SimpleEntity
      • Special Cases
    • IterableEntity & Iterable2Entity
      • IterableEntity / Iterable2Entity kinds
      • The Type Template
      • Creating an IterableEntity / Iterable2Entity
      • InvalidMembers Description
      • Special Cases
      • Creating your own IterableEntity / Iterable2Entity kind
  • Advanced Notions
    • The NullFailure Annotation
    • Using multiple parameters annotations
    • Class Hierarchy of ValueObjects
    • Class Hierarchy of Entities
    • Models that are always valid / invalid
  • Union of Modddels
    • Union of Modddels Overview
    • Basic Usage
    • Case-modddels pattern matching
    • Default Factory Constructor
    • Shared Properties
    • The Validate Methods
    • CopyWith
  • Unit-Testing Of Modddels
    • Unit-Testing Overview
    • Available Tests
    • Customizing Tests
  • Additional Information
    • Using type aliases
    • Re-running the generator
    • All Available Modddels
    • VsCode Snippets
Powered by GitBook
On this page
  1. Unit-Testing Of Modddels

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.

PreviousCopyWithNextAvailable Tests