2016-09-06 2 views
2

벨로우는 예제이며 test failed-str은 정의되지 않았습니다. angular2의 약속에서 얻는 가치는 무엇입니까?테스트 약속 <string> 각도 2 단위 테스트의 값

describe('Test',() => { 
     it('case of string',() => { 
      let t: Promise<string> = deserializeSimpleField(...); 
      let str:string; 
      t.then(value=>str = value); 
      expect(str).toEqual('name'); 
     });}); 

답변

3

비동기 실행은 전염성이 있습니다. 동기화 실행으로 돌아갈 수 없습니다.

describe('Test',() => { 
    it('case of string', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { 
     deserializeSimpleField(...) 
     .then(value => { 
      expect(value).toEqual('name'); 
      async.done(); 
     }); 
    }); 
}); 

나는

갱신

describe('Test',() => { 
    it('case of string', async(() => { 
     return deserializeSimpleField(...) 
     .then(value => { 
      expect(value).toEqual('name'); 
      async.done(); 
     }); 
    })); 
}); 
+0

친애하는 내가 각도/테스트 @ 사용 @Gunter 및 AsyncTestCompleter가없는 https://github.com/angular/angular/blob/master/modules/@angular/forms/test/form_array_spec.ts에서 그것을 보았다? 존재하는 방법은 @ 각진/테스트로합니까? – emanuel07

+0

실제로 비동기 테스트를 수행하는 것이 정확하고 현재의 방법이 무엇인지 알지 못합니다. 나는 changelog에서 아무것도 찾을 수 없었다. 최근에 업데이트 된 github 저장소에서 테스트를 찾았습니다. 내 업데이트 된 답변을 시도해 주시겠습니까? (추가 된'return'이 필요한지 확실하지 않음). –

+0

업데이트 된 버전 가져 오기 : TypeError : AsyncTestZoneSpec이 (가) 생성자가 아닙니다 at runInAsyncTestZone – emanuel07

2
I resolve question following way,(using fakeAsync,tick..) 

require('zone.js/dist/fake-async-test'); 
import {describe, it, expect, fakeAsync, tick} from '@angular/testing' 
describe('my first test',() => { 
    it('Promises fulfilled by tick',fakeAsync((): void => { 
     let promise:Promise<number> =Promise.resolve(11); 
     let x:number; 
     promise.then(v => { 
      x = v; 
     }); 
     tick(); 
     expect(x).toBe(11); 
    })); 
}); 
+0

친애하는 @Gunter는 해결책을 참조하십시오. – emanuel07

+0

유일한 차이점은 '가짜'인 것 같습니다. 맞습니까? (그리고'tick();') –

+0

예. tick() - fakeAsync 영역의 타이머에 대한 비동기식 시간 경과를 시뮬레이션합니다. – emanuel07