how to write a meaningful test for `extending a class`?

200

I suppose super(data) ist passing the constructor parameter to the constructor of the base class (I do not know a lot about dart).

You can only test if super was called by observing its behavior. You have to check the base classes constructor for what it is doing with the parameter and then see if you can observe if that happened (if possible). Example: If the base class constructor is assigning the parameter to a field that you can access from the outside, you can assert on that parameter being set. This only works if

  1. You can access the field from your test code. Access in this context does not mean to access it directly or via a getter. Any means of observing that the field has been set should be sufficient.
  2. The class under test is not setting the field itself (there is probably no way to decide which constructor set the field).
Share:
200
Francesco Iapicca
Author by

Francesco Iapicca

Self taught developer in love with Flutter and Dart

Updated on December 27, 2022

Comments

  • Francesco Iapicca
    Francesco Iapicca over 1 year

    what is the correct way to write a meaningful test for extending a class with the super keyword?

    class FooBase<T> {
      final T data;
      const FooBase(this.data);
    }
    
    class Foo<T> extends FooBase<T> {
      const Foo(T data)
          : super(data);
    }