如何编写一个有意义的测试来扩展类

如何编写一个有意义的测试来扩展类

本文介绍了如何编写一个有意义的测试来扩展类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扩展a编写有意义的测试的正确方法是什么使用 super 关键字?

class FooBase<T> {
  final T data;
  const FooBase(this.data);
}

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

推荐答案

我认为 super(data) ist将构造函数参数传递给基类的构造函数(我不太了解 dart ).

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

您只能通过观察其行为来测试是否调用了 super .您必须检查基类构造函数对参数的作用,然后查看是否可以观察到这种情况是否发生(如果可能).示例:如果基类构造函数正在将参数分配给您可以从外部访问的字段,则可以对所设置的参数进行断言.仅在以下情况下有效

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. 您可以从测试代码访问该字段.在这种情况下,访问并不意味着直接访问它或通过获取器访问它.任何观察到该字段已设置的方法都应足够.
  2. 被测类没有设置字段本身(可能无法决定哪个构造函数设置字段).
  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).

这篇关于如何编写一个有意义的测试来扩展类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:49