Angular Jasmine中的beforeach in测试中定义的访问常量
对于给定的代码,如何访问testAngular Jasmine中的beforeach in测试中定义的访问常量,angular,unit-testing,jasmine,karma-runner,Angular,Unit Testing,Jasmine,Karma Runner,对于给定的代码,如何访问testmyTest中的constmyConst: describe('HeaderCustomerDetailsComponent child components',() => { beforeEach(() => { ... const myConst = "Something"; }); it('myTest', () => { expect(myConst).toBeTruthy(); }); });
myTest
中的constmyConst
:
describe('HeaderCustomerDetailsComponent child components',() => {
beforeEach(() => {
...
const myConst = "Something";
});
it('myTest', () => {
expect(myConst).toBeTruthy();
});
});
因为您正在
beforeach(…)
方法中定义myConst
,所以它被限制在该范围内。最好的方法是将myConst
移动到类级别,并使用let
定义它
试试这个:
describe('HeaderCustomerDetailsComponent child components',() => {
let myConst;
beforeEach(() => {
...
this.myConst = "Something";
});
it('myTest', () => {
expect(this.myConst).toBeTruthy();
});
});
由于您仍然在每个(…)之前的
方法中设置myConst
,因此您可以避免测试之间的污染,但是您仍然应该小心。谢谢!尽管我不得不这样做:constmyconst:myConstType={…someting};beforeach(()=>{…//myConst.newProperty='maybe';});它('myTest',()=>{expect(myConst).toBeAwesome();})代码>