[英]Dart, Why my class has no error when I implements a class without override every field, But extends Mock from Mockito?
I'm in Dart.我在 Dart。
Why my class has no error when I implements a class without override every field, But extends Mock from Mockito?为什么我的 class 在我实现 class 而没有覆盖每个字段但从 Mockito 扩展 Mock 时没有错误?
For example,例如,
class Base {
void a() {}
void b() {}
}
// NO ERROR
class ConcreteA implements Base {
@override
void a() {}
@override
void b() {}
}
// ERROR: Didn't override every field.
class ConcreteB implements Base {
@override
void a() {}
}
// NO ERROR
class ConcreteC extends Mock implements Base {
@override
void a() {}
}
How could Mockito implement this? Mockito 是如何实现的呢?
The Mock class implements Base 'magically' here because of the Mock overrides the noSuchMethod method . Mock class 在这里“神奇地”实现了 Base,因为 Mock 覆盖了noSuchMethod 方法。
dynamic noSuchMethod(Invocation invocation, [Object /*?*/ returnValue]) {
// noSuchMethod is that 'magic' that allows us to ignore implementing fields
// and methods and instead define them later at compile-time per instance.
// See "Emulating Functions and Interactions" on dartlang.org: goo.gl/r3IQUH
The methods from Base are actually implemented by Mock at compile time.来自 Base 的方法实际上是在编译时由 Mock 实现的。
Thus, since ConcreteC is a subclass of Mock, no error should be shown.因此,由于 ConcreteC 是 Mock 的子类,因此不应显示任何错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.