2017-08-04 151 views
1

角度单元测试是新功能。角度单元测试:如何运行基本测试

我想实现第一运行试验,所以我开发了这个:

import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 
import { AppComponent } from './app.component'; 

describe('AppComponent',() => { 
    let component: AppComponent; 
    let fixture: ComponentFixture<AppComponent>; 

    beforeEach(async(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ AppComponent ], 
    }).compileComponents(); 
    })); 

    beforeEach(() => { 
    fixture = TestBed.createComponent(AppComponent); 
    component = fixture.componentInstance; 
    fixture.detectChanges(); 
    }); 

    it('first test',() => { 
    expect('1').toBe('1'); 
    }); 
}); 

就像你看到我的第一个测试是断言,“1”是“1”,我不知道为什么我面对得到它全成的问题,因为它引发了我这样的错误:

Error: Template parse errors: Can't bind to 'min' since it isn't a known property of 'dx-progress-bar'. 
    1. If 'dx-progress-bar' is an Angular component and it has 'min' input, then verify that it is part of this module. 
    2. If 'dx-progress-bar' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. 
    3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. (" 
     width="100%" 
     [class.complete]="progressBar.value == maxValue" 
     [ERROR ->][min]="0" 
     [max]="maxValue" 
     [statusFormat]="wordProgression" "): ng:///DynamicTestModule/[email protected]:4 

这是真的,我使用DevExtreme小部件在我的应用程序组件,但我还没有甚至试图对它进行测试。我只是从明显的测试案例开始。

我需要知道我该如何解决它?

建议?

回答

2

你必须包括需要编译你的组件在你TestBed.configureTestingModule所有的东西:

// import { DevExtremeModule } from 'devextreme-angular'; 
import { DxProgressBarModule } from 'devextreme-angular/ui/progress-bar'; 


TestBed.configureTestingModule({ 
    imports: [ 
    DxProgressBarModule, 
    // or DevExtremeModule 
    ], 
    declarations: [ AppComponent ], 
}).compileComponents(); 
+0

基本上,intented什么yurzui说的是,你需要实际重新建立一个动态模块,以测试你的组件。原因是因为所有东西都与依赖注入与Angular相关联,并且您必须使用TestBed.configureTestingModule()来重新创建一个模块,以提供组件编译所需的最低限度要求。 如果你需要服务/管道,或者其他类似的东西,不要忘记把你的课程“存根”作为嘲笑使用。 –

+0

@AlexBeugnet,非常感谢这些信息,因此我想知道如何存根/模拟我的服务? – firasKoubaa

+0

类似于'{提供:MyService,useClass:MyServiceMock}' – yurzui