我想在Webstorm中通过Karma ng test test-login --karma-config
运行我的测试,但是它显示出已经运行了0个测试。我的代码有误吗?如何进行行为测试?
mocked-login.service.ts
import {Injectable} from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class MockedLoginService {
authenticated = false;
constructor() {
}
isAuthenticated() {
return !!localStorage.getItem('token');
}
authenticate() {
return 'start auth...';
}
}
mocked-login.service.spec.ts
import {TestBed} from '@angular/core/testing';
import {MockedLoginService} from './mocked-login.service';
describe('Service: Login', () => {
let service: MockedLoginService;
beforeEach(() => {
service = new MockedLoginService();
});
afterEach(() => {
service = null;
localStorage.removeItem('token');
});
it('should return the string <<start auth...>> from authenticate when user starts the authentication', () => {
expect(service.authenticate).toEqual('start auth...');
});
it('should return true from isAuthenticated when there is a token', () => {
localStorage.setItem('token', '1234');
expect(service.isAuthenticated()).toBeTruthy();
});
it('should return false from isAuthenticated when there is no token', () => {
expect(service.isAuthenticated()).toBeFalsy();
});
});