简体   繁体   English

角注入式部件的Karma / Jasmine单元测试

[英]Karma/Jasmine Unit Test of Component with Injected Service in Angular

I am new to Karma/Jasmine unit testing, and I am attempting to write some tests for my authentication component that has an AuthService injected into it. 我是Karma / Jasmine单元测试的新手,我正尝试为已插入AuthService的身份验证组件编写一些测试。 I also have another authentication service that is used for Microsoft (Azure) authentication injected into the general AuthService. 我还具有另一种身份验证服务,该服务用于一般身份验证服务中注入的Microsoft(Azure)身份验证。 For building my spec, I have: 为了建立我的规格,我有:

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
  });

})

There are not any tests currently because I wanted to make sure everything is getting set up correctly. 当前没有任何测试,因为我想确保所有设置都正确。 However, when I run npm test , I receive: 但是,当我运行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]   }

I don't particularly understand where I've gone wrong. 我不太了解自己哪里出了问题。 Apparently, it's not finding that .js file, but that .js file does exist in that location and works fine when I run the Angular app normally. 显然,它找不到该.js文件,但是该.js文件确实存在于该位置,并且在我正常运行Angular应用时可以正常工作。 So I just assume I'm setting up the test incorrectly? 所以我只是假设我没有正确设置测试? Any help would be appreciated. 任何帮助,将不胜感激。 I've been attempting to follow the Angular documentation on testing. 我一直在尝试遵循Angular文档进行测试。 If this would be helpful, this is how the beginning of my AuthService looks: 如果这会有所帮助,则这是我的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) {}

Solved this by editing the karma.conf.js file and adding to the files:[] 通过编辑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 },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM