【发布时间】:2018-11-26 08:17:33
【问题描述】:
我拿了常用模块globals.js中常用的测试:
const user = {
username: 'Skat',
password: '123',
role: 'user',
};
function relogin(user, authService) {
describe('--> relogin', () => {
it('--> ' + user.role + ', ' + user.username, async () => {
authService.logout();
expect(authService.isLogged()).toBe(false);
authService.login(user.username, user.password).subscribe();
await delay(800);
expect(authService.isLogged()).toBe(true);
});
});
}
module.exports.user = user;
module.exports.relogin = relogin;
从service.spec.ts 调用共享测试:
const globals = require('../globals');
describe('AdvertService', () => {
let authService: AuthService;
beforeEach(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
TestBed.configureTestingModule({
imports: [HttpClientModule, RouterTestingModule],
providers: [
AuthService
],
});
});
it('Injected services should be created', inject([AuthService], async (authService1: AuthService) => {
expect(authService).toBeFalsy();
authService = authService1;
expect(authService).toBeTruthy();
}
));
globals.relogin(globals.user, authService);
});
因此,来自公共模块的测试失败,原因是:
TypeError: 无法读取未定义的属性“注销”
但是服务对象肯定是创建的,只是因为某种原因undefined来到了公共模块。
请告诉我如何通过测试中的服务对象,我在公共模块中移动?
【问题讨论】:
标签: javascript angular jasmine