kub*_*003 3 jasmine typescript
我将 Jasmine 与 Typescript 一起使用,最近我们开始使用和this中的上下文。beforeEachit
例子:
beforeEach(() => {
this.fixture = TestBed.createComponent(blablabla);
});
it('should do something', () => {
expect(this.fixture).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)
问题是 TypeScript 不够聪明,无法弄清楚thisinside与 inbeforeEach完全相同。有谁知道一种简单的方法来“暗示”打字稿这个事实?thisit
这可能吗?
您可以在函数中键入提示this。实际上,如果您只使用箭头函数(在describe、beforeEach和 中it),this我想上下文将是最外层的全局上下文。由于该函数无法注释,因此我建议将常规旧式函数传递到最外层describe:
// Dummy-type Jasmine functions (only for this MWE)
declare const describe: (msg: string, fun: () => void) => void;
declare const it: (msg: string, fun: () => void) => void;
declare const beforeEach: (fun: () => void) => void;
class A {
aProperty: string;
}
interface TestSuiteContext {
myObj: A;
}
describe('Test suite', function (this: TestSuiteContext) {
beforeEach(() => {
this.myObj = new A();
});
it('should do something', () => {
const message: string = this.myObj.aProperty;
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
592 次 |
| 最近记录: |