the current project and code structure are as follows
- class A file (root)
+ class B file
- class C file
// TypeScript
class A {
private b: B;
constructor(options) {
this.b = new B(this);
}
otherMethod() {}
}
class B {
private a: A;
private c: C;
constructor(root: A) {
this.a = root;
this.c = new C(root);
}
otherMethod() {}
}
class C {
private a: A;
constructor(root: A) {
this.a = root;
}
handler(param1: boolean, param2: any) {
if (param1) {
this.a.otherMethod();
} else {
this.otherMethod();
}
}
otherMethod() {}
}
now to test the handler method in C, in order to protect handler from other environments, you should use the root parameter of stub/mock C and the C.otherMethod method called in handler, and then const c = new C (mockedA); c.handler (mockedP1, mockedP2);
what is a little puzzling at the moment is how to stub/mock the root parameter of C?
the first thing that comes to mind is that an instance of An in stub/mock is passed to C, and doing so is equivalent to new an instance of the entire project, which sounds a little wrong.
the second thing that comes to mind is to construct a class equivalent to A. the problem with this is that if An is very large, it is quite troublesome to construct.
in TypeScript, the passed parameters must match the interface to ensure that there are no type errors, so it is stuck here.
also because of another problem caused by the type, how to test the private methods in the class?
Please ask the students who understand the Typescript test to answer the questions. Thank you.