对于给定的代码,如何在测试myTest
中访问常量myConst
:
describe('HeaderCustomerDetailsComponent child components',() => {
beforeEach(() => {
...
const myConst = "Something";
});
it('myTest', () => {
expect(myConst).toBeTruthy();
});
});
因为您要在beforeEach(...)
方法中定义myConst
,所以它仅限于该范围。最好的方法是可能将myConst
移到类级别,并使用let
定义它。
试试这个:
describe('HeaderCustomerDetailsComponent child components',() => {
let myConst;
beforeEach(() => {
...
this.myConst = "Something";
});
it('myTest', () => {
expect(this.myConst).toBeTruthy();
});
});
由于您仍在beforeEach(...)
方法中设置myConst
,因此您可以避免测试之间的污染,但您仍然应该小心。