Angular 2测试 - 服务不会注入

时间:2016-04-29 20:16:43

标签: unit-testing angular jasmine

我正在使用angular 2和jasmine来尝试测试几个服务,一个服务依赖于另一个服务。我一直收到服务提供商错误。

import {Injectable}     from 'angular2/core';

@Injectable()
export class ServiceA {
  constructor() {
  }
}

serviceA的规范

import {it, 
  describe, expect, 
  beforeEach, 
  beforeEachProviders, inject}
from 'angular2/testing';
import {ServiceA} from './serviceA;

describe('ServiceA Tests', () => {
  let service: ServiceA;

  beforeEachProviders(() => {
    return [
        ServiceA
    ]
  });
  beforeEach(inject([ServiceA], (l) => {
    service = l;
  }));

  it('Service Created', () => {
    expect(service).toBeDefined();
  });

});

配置类

import {OpaqueToken} from 'angular2/core';

export let APP_CONFIG = new OpaqueToken('app.config');

export interface Config {
  applicationBaseUrl: string;    
}

export const CONFIG: Config = {
  applicationBaseUrl: 'some value',
};

服务b

import { Injectable,Inject}     from 'angular2/core';
import { Http,Response,RequestOptions,Headers} from 'angular2/http';
import {APP_CONFIG, Config,CONFIG}    from './app.config';
import {ServiceA} from './serviceA';

export interface IServiceB {
}

@Injectable()
export class ServiceB implements IServiceB {
  constructor(private _http: Http,@Inject(APP_CONFIG)
       private _config:Config,private serviceA: ServiceA) {

  }

}

服务B的规范

import {it, describe, expect, beforeEach, beforeEachProviders,
inject} from 'angular2/testing';
import {ServiceB} from './serviceB';
import {ServiceA} from './serviceA';
import {HTTP_PROVIDERS, Http, Response, RequestOptions,
Headers}   from 'angular2/http';
import {APP_CONFIG, Config, CONFIG}    from './app.config';
import {provide} from 'angular2/core';

import 'rxjs/Rx'; // Add all operators to Observable

describe('ServiceB Tests', () => {
  let serviceB: ServiceB;
  let appConfig: Config;
  let http: Http;
  let serviceA: ServiceA;
  beforeEachProviders(() => {
    return [
        HTTP_PROVIDERS,
        provide(APP_CONFIG, { useValue: CONFIG }),
        ServiceA,
        ServiceB
    ]
  });
  beforeEach(inject([APP_CONFIG, Http, ServiceA,ServiceB], (ac, h, a,b) => {
    appConfig = ac;
    http = h;
    serviceA = a;
    service = b; // new ServiceB(http, appConfig, serviceA);
    appConfig.applicationBaseUrl = '/';

  }));

  it('Http created', () => {
    expect(http).toBeDefined();
  });

  it('service a created', () => {
    expect(serviceA).toBeDefined();
  });

  it('App config created', () => {
    expect(appConfig).toBeDefined();
  });

  it('service B created', () => {
    expect(serviceB).toBeDefined();
  });

});

服务A加载并运行正常。如果我手动创建ServiceB的东西工作,但如果我尝试注入ServiceB,我会收到错误。

  

失败:没有ServiceA的提供商! (ServiceB-> ServiceA)

它创建了ServiceA,所以不确定为什么它说没有提供者。

1 个答案:

答案 0 :(得分:1)

我尝试使用您的代码,它适用于我关于beta15的依赖注入。请参阅此plunkr:https://plnkr.co/edit/ZgCgNh?p=preview

您的ServiceB代码中有一个小错字:

beforeEach(inject([APP_CONFIG, Http, ServiceA,ServiceB], (ac, h, a,b) => {
  appConfig = ac;
  http = h;
  serviceA = a;
  serviceB = b; // <-------------
  appConfig.applicationBaseUrl = '/';
}));

您能告诉我更多关于您使用的环境的信息吗?谢谢!