2017-12-02 3 views
0

서비스에서 디스패치 방법을 잡을 수 없습니다. 내가 아는 한, 문제는 Observable.subscribe()에 필요한 디스패치이기 때문입니다. 여기ng2-redux (angular-redux)를 사용한 각도 4.4 테스트 서비스

는 서비스 :

여기
@Injectable() 
export class AuthUserBackendService extends BaseBackendDataService{ 

constructor(
private http: Http, 
private tokenLoader: TokenLoaderBackendService, 
private ngRedux: NgRedux<IAppState>, 
private logger: LoggerService, 
private errorService: BackendDataErrorsService 
) { 
    super(http, tokenLoader, ngRedux, logger, errorService); 
} 
........ 
testMethod() { 

// this dispatch ok 
// this.ngRedux.dispatch({type: 
ActiontionsBackendData.authUserBackendActions.startHttpRequestAuthUser}); 

Observable.of({Id: 'testId', Name: 'TestName'}).delay(100).subscribe(
    (result) => { 

    const payload = {authUser: result}; 

    // this dispatch I can't catch in test 
    this.ngRedux.dispatch({type: ActionsBackendData. authUserBackendActions. 
    receivedAuthUserHttpResponseSuccess, payload: payload}); 
    }); 
} 
} 

가 내 .spec.ts 처음 파견 주석을 제거하면 나는 동일한 1에 0을 예상 얻을 그래서 '캐치 파견 전화 카운트'에 대한

let testService: AuthUserBackendService; 
let mockState: any = { 
authUserState: { 
    authUser: null 
} 
}; 
let mockRedux: NgRedux<IAppState> = new MockRedux(mockState); 


describe('AuthUserBackendService',() => { 

beforeEach(() => { 
TestBed.configureTestingModule({ 
    imports: [ 
    ToastyModule, 
    TranslateModule.forRoot(), 
    NgReduxModule, 
    LoggerModule, 
    MockBackendModule, 
    RouterTestingModule.withRoutes([]), 
    BackendDataModule, 
    CoreModule 
    ], 
    providers: [ 
    {provide: Router, useClass: RouterTestingModule}, 
    {provide: NgRedux, useValue: mockRedux}, 

    ] 
}); 



    testService = TestBed.get(AuthUserBackendService); 

}); 

    it('should be created',() => { 
    expect(testService).toBeTruthy(); 
    }); 

describe('testMethod()',()=>{ 

it('catch dispatch calls count',()=> { 

    const spy = spyOn(mockRedux, 'dispatch'); 

    testService.testMethod(); 

    expect(spy.calls.count()).toEqual(1); 

    }); 

}); 

}); 

, 테스트는 괜찮을 것이지만, 구독 영역에서 디스패치를 ​​잡을 수 없다는 의미입니다. 뭐가 잘못 되었 니? 조언 해 주셔서 고맙습니다.

답변

0

나는이 문제를 해결했다. 다음은 스펙에 대한 올바른 코드입니다. 어쩌면 또 다른 방법이 있을지 모르지만 찾았습니다.

.... 

describe('testMethod()',()=>{ 

    it('catch dispatch calls number', fakeAsync(()=> { 

    const spy = spyOn(mockRedux, 'dispatch'); 

    testService.testMethod(); 

    tick(120); // in general it must be more then delay 

    expect(spy.calls.count()).toEqual(1); 

    })); 


});