我正在为我的AccountLoginService写一个UTC。在它的构造函数中,它基于环境获取实例。如果environment为true,则获取AccountLoginMockService的实例,否则为AccountLoginApiService。由于我已将我的env设置为true,因此它始终获取AccountLoginMockService的实例。
import { Injectable, Injector, EventEmitter } from '@angular/core'
import { environment } from '../../../../environments/environment';
import { Observable } from 'rxjs/Observable';
import { AccountLoginMockService } from './accountLogin.service.mock';
import { AccountLoginApiService } from './accountLogin.service.api';
import { AccountLogin } from '../accountLogin';
import { CacheCustomService } from '../../../shared/services/cache/cacheCustom.service';
import { UserDetail } from './accountLogin.model';
import { InstanceService } from '../../../shared/services/instance.service';
export interface IAccountLoginService {
singinUser(userDetails: AccountLogin): Observable<UserDetail>,
logout(): void,
isAuthorized(): boolean,
};
@Injectable()
export class AccountLoginService implements IAccountLoginService {
loginService: IAccountLoginService;
cacheService: CacheCustomService;
loginStatusEmitter: EventEmitter<UserDetail> = new EventEmitter<UserDetail>();
/**
* Constructor
* @param {InstanceService} privateinstanceService
*/
constructor(private instanceService: InstanceService) {
this.loginService = this.instanceService.getInstance((environment.needMock) ?
AccountLoginMockService : AccountLoginApiService);
this.cacheService = this.instanceService.getInstance(CacheCustomService);
};
;
/**
* Calls signin function of concerned service
* @param {} userDetails
* @returns Observable
*/
singinUser(userDetails: AccountLogin): Observable<UserDetail> {
return this.loginService.singinUser(userDetails).map((data) => {
this.storeUserDetail(data);
return data;
});
};
/**
* Calls logout function of concerned service
* @returns void
*/
logout(): void {
this.cacheService.deleteCacheKey('userDetail');
return this.loginService.logout();
};
/**
* calls isAuthorized function of concerned service
* @returns boolean
*/
isAuthorized(): boolean {
return this.loginService.isAuthorized();
};
/**
* Stores user details in cache and emits to login status component
* @param {UserDetail} userDetail
*/
private storeUserDetail(userDetail: UserDetail) {
this.cacheService.storeDataToCache(userDetail, 'userDetail', false)
this.loginStatusEmitter.emit(userDetail);
}
};
当我写它的testCase时,我得到以下错误
错误:没有AccountLoginMockService的提供者!
虽然我把它包含在任何需要的地方。请看下面的UTC,让我知道我哪里出错了
import { AccountLoginService } from './accountLogin.service'
import { ReflectiveInjector, EventEmitter } from "@angular/core";
import { InstanceService } from "app/shared/services/instance.service";
import { TestBed } from "@angular/core/testing";
import { AccountLoginApiService, AccountLoginMockService } from "app/pages/accountLogin/accountLoginService";
import { CacheCustomService } from "app/shared/services/cache/cacheCustom.service";
import { AccountLoginModule } from "app/pages/accountLogin/accountLogin.module";
import { Observable } from "rxjs/Observable";
import { HttpModule } from "@angular/http";
describe('Account Login Service', () => {
let accountLoginService : AccountLoginService,
mockCacheService: CacheCustomService ,
mockInstanceService :InstanceService,
mockAccountLoginApiService ={};
beforeEach(async() =>{
let mockAccountLoginService = {
singinUser :(userDetails)=>{
if(userDetails.userName === 'abc@123.com' && userDetails.password === '123456'){
return Observable.of(true);
} else {
return Observable.of(false);
}
}
}
TestBed.configureTestingModule({
providers : [
{ provide: AccountLoginMockService, useValue: mockAccountLoginService },
//{ provide: AccountLoginApiService, useValue: mockAccountLoginApiService },
InstanceService, AccountLoginApiService, AccountLoginMockService , AccountLoginApiService]
});
})
beforeEach(() => {
const injector = ReflectiveInjector.resolveAndCreate([InstanceService])
mockInstanceService = injector.get(InstanceService);
accountLoginService = new AccountLoginService(mockInstanceService);
})
it('should be able to login' ,()=>{
let userDetails = { userName:'intershop@123.com', password: '123456'}
accountLoginService.singinUser(userDetails).subscribe( (data)=>{
console.log(data);
expect(data).toBeDefined;
})
});
});