无法使用Jasmine和Karma运行Angular测试

时间:2019-05-10 12:39:34

标签: angular typescript jasmine karma-runner

我想在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();
      });

    });

enter image description here

0 个答案:

没有答案