2016-07-20 3 views
0

나는 angular-cli를 기반으로하는 프로젝트에 대해 매우 간단한 샘플 테스트를 만들었습니다. 제목에서 알 수 있듯이 TestComponentBuilder.createAsync()가 약속을 해결하지 못하는 이유가 문제입니다. 여기 내 코드가있다. 나는이 문제가 karma-test-shim.js 설정 파일에 있다고 의심하지만 이것에 대해서는 잘 모르겠습니다. 테스트 결과는 필자가 테스트를 위해 "expect (true) .toEqual (false)"로 설정했지만 항상 성공합니다. 난 당신이 async으로 inject 기능 비동기 테스트를 포장 할 필요가 각 2 RC4에서 각도 2 RC4Angular-CLI TestComponentBuilder.createAsync()가 약속을 해결하지 못함

import { 
beforeEach, 
beforeEachProviders, 
describe, 
expect, 
it, 
inject 
} from '@angular/core/testing'; 
import { ComponentFixture, TestComponentBuilder } from '@angular/core/testing'; 

import { Component } from '@angular/core'; 
import { By } from '@angular/platform-browser'; 

describe('Component: CollectionCounterWidgetComponent',() => { 

let builder: TestComponentBuilder; 
beforeEach(inject([TestComponentBuilder], function (tcb: TestComponentBuilder) { 
builder = tcb; 
})); 

it('should create the CollectionCounterWidgetComponent component', inject([],() => { 
return builder.createAsync(ComponentTestController) 
    .then((fixture: ComponentFixture<any>) => { 
    fixture.detectChanges(); 
    expect(true).toEqual(false); 
    }); 

})); 
}); 

@Component({ 
selector: 'test', 
template: ` 
<h1>why?</h1> 
` 
}) 
class ComponentTestController { 
} 

답변

0

함께 일하고 있어요. 그러면 AsyncTestZoneSpec에서 테스트가 실행되고이 영역 내의 모든 비동기 호출이 완료되었는지 확인합니다. 귀하의 경우에는

당신이 import {async} from '@angular/core/testing'을해야하고 테스트 수정 : 예상대로

it('should create the CollectionCounterWidgetComponent component', async(inject([],() => { 

    builder.createAsync(TestControllerComponent) 
     .then((fixture: ComponentFixture<any>) => { 
     fixture.detectChanges(); 
     expect(true).toEqual(false); 
     }); 

    })) 
); 

지금 테스트가 실패합니다. 또한 환불 명세서가 필요하지 않습니다 (예 : 반환 건축업자 ...)

0

오케이 마이클, 내 잘못 이었지만 문제는 남아 있습니다. 나는 깨끗한 프로젝트를 만들기 위해 각기둥 형 (angular-cli)을 가진 또 다른 프로젝트를 만들었으며 다음과 같은 간단한 테스트를 만들었습니다.

import { 
async, 
inject, 
describe, 
it, 
expect, 
TestComponentBuilder, 
ComponentFixture 
} from '@angular/core/testing'; 
import { Component } from '@angular/core'; 

import { AppComponent } from './app.component' ; 

describe('Component: AppComponent',() => { 

it('should create an instance', async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { 
    tcb.createAsync(AppComponent) 
    .then(fixture => { 
    expect(true).toBe(false); 
    }) 
}))); 
}); 

그러나 테스트가 성공적으로 수행되었습니다. 기본적으로 angular-cli는 현재 버전에서 RC3을 사용하고 RC4에서는 각도 버전을 업데이트했습니다. 위의 테스트 "asynchronous (inject .."및 각도 RC3 대신 "injectAsync"같은 프로젝트에서 테스트 한이 경우 제대로 작동합니다. 감사합니다