角度注入服务的组件的业力/茉莉花单元测试

时间:2017-06-27 00:26:16

标签: angular unit-testing karma-jasmine adal

我是Karma / Jasmine单元测试的新手,我正在尝试为我的身份验证组件编写一些测试,其中注入了一个AuthService。我还有另一个身份验证服务,用于注入通用AuthService的Microsoft(Azure)身份验证。为了建立我的规范,我有:

describe('AuthAdminComponent', function () {
  let de: DebugElement;
  let comp: AuthAdminComponent;
  let fixture: ComponentFixture<AuthAdminComponent>;
  let authService: AuthService;

  let authServiceStub: {
    // Fill in later
  }

    beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ AuthAdminComponent ], // declare the test component
      providers: [
        {provide: AuthService, useValue: authServiceStub},
      ]
    })
    .compileComponents(); // compile template and css
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(AuthAdminComponent);
    comp = fixture.componentInstance;
    authService = TestBed.get(authService) // Get the needed service
  });

})

目前没有任何测试,因为我想确保一切都正确设置。但是,当我运行npm test时,我会收到:

404: /base/node_modules/ng2-adal/core.js
Chrome 59.0.3071 (Windows 10 0.0.0) ERROR

{
[1]     "originalErr": {
[1]       "__zone_symbol__currentTask": {
[1]         "type": "microTask",
[1]         "state": "notScheduled",
[1]         "source": "Promise.then",
[1]         "zone": "<root>",
[1]         "cancelFn": null,
[1]         "runCount": 0
[1]       }
[1]     },
[1]     "__zone_symbol__currentTask": {
[1]       "type": "microTask",
[1]       "state": "notScheduled",
[1]       "source": "Promise.then",
[1]       "zone": "<root>",
[1]       "cancelFn": null,
[1]       "runCount": 0
[1]     }
[1]   }

我不太明白我哪里出错了。显然,它没有找到.js文件,但该.js文件确实存在于该位置,并且在我正常运行Angular应用程序时工作正常。所以我只是假设我错误地设置了测试?任何帮助,将不胜感激。我一直在尝试按照Angular文档进行测试。如果这会有所帮助,那么我的AuthService的开头就是这样的:

import {Injectable} from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';
import { AdalService } from 'ng2-adal/core';

@Injectable()
export class AuthService {

    public authType: string = null;
    public connection: string = null;
    public accessToken: string = null;
    public refreshToken: string = null;

    constructor(private adalService: AdalService, private http: Http) {}

1 个答案:

答案 0 :(得分:0)

通过编辑karma.conf.js文件并添加到文件来解决这个问题:[]

{ pattern: 'node_modules/ng2-adal/**/*.js', included: false, watched: false },
{ pattern: 'node_modules/ng2-adal/**/*.js.map', included: false, watched: false },
{ pattern: 'node_modules/adal-angular/**/*.js', included: false, watched: false },
{ pattern: 'node_modules/adal-angular/**/*.js.map', included: false, watched: false },