export let AUTH_SERVICE = new OpaqueToken('auth.service');
export interface AuthService {
logIn(): void;
logOut(): void;
}
在我的测试类中定义的接口和不透明的令牌我提供的AuthService
一个存根版本,即
@Injectable()
class AuthServiceStub implements AuthService {
logIn(): void {}
logOut(): void {}
}
,并设置我的测试beforeEach
如下
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ],
providers: [
{provide: AUTH_SERVICE, useValue: AuthServiceStub}
]
});
}));
然后我开始编写测试,即
it('should call log in on AuthService',() => {
let authService = fixture.debugElement.injector.get(AUTH_SERVICE);
spyOn(authService, 'logIn');
// expect will go here
});
,但我得到以下错误
Error: <spyOn> : logIn() method does not exist
看不到我在做什么错。有任何想法吗?
如果这是一个运行时错误,您可以检查您的ide或浏览器中的'authService'实例以查看是否存在'logIn'方法。 – Igor
还没有让我的脑袋适当地调试这些测试。然而,如果我将一个构造函数添加到'AuthServiceStub'并写入到该构造函数的控制台中,例如'console.log('CREATED')',它从不向控制台输出'CREATED' - 所以不知道我的存根服务是否曾经得到创造.... –