2016-04-29 109 views
1

我正在使用角2和茉莉來嘗試和測試兩個服務,一個服務是依賴於另一個。我不斷收到服務提供者錯誤。角2測試 - 服務不會注入

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

我試了一下你的代碼,它適用於我的依賴注入與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 = '/'; 
})); 

你能告訴我一些有關環境你使用?謝謝!

+0

我使用visual studio 2015,angular beta 15,typescript 1.8.9,jasmine 2.4.1,你的測試看起來是正確的,但這不是我得到的。我可以將服務A注入另一服務C但不能將服務B注入服務C –

+0

好吧!謝謝。 PLUNK的版本和我一樣嗎?你有可能與我分享一些東西來重現(plunkr或github存儲庫)嗎? –

+0

我把我的實際服務放在這裏https://plnkr.co/edit/ft7VJ8PMptJbyFdcc4RX?p=preview –