2017-07-13 68 views
0

我正在爲運行Angular應用程序獲取初始測試集,並且遇到其中一個依賴關係時遇到問題。爲測試規範看到代碼:編譯測試臺時出現「錯誤:無法解析Alert的所有參數」

import { TestBed, async } from '@angular/core/testing'; 
import { AppComponent } from './app.component'; 
import { NavbarComponent } from './navbar/navbar.component'; 
import { RouterModule, Routes } from '@angular/router'; 
import { Alert } from './models/alert.model'; 
import { AlertsService } from './services/alerts.service'; 


describe('AppComponent',() => { 
    beforeEach(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ 
     AppComponent, NavbarComponent 
     ], 
     imports: [ 
     RouterModule, 
     ], 
     providers: [ 
     Alert, AlertsService 
     ] 
    }); 
    TestBed.compileComponents(); //This appears to be what throws the error 
    }); 

    it('should create the app', async(() => { 
    const fixture = TestBed.createComponent(AppComponent); 
    const app = fixture.debugElement.componentInstance; 
    expect(app).toBeTruthy(); 
    })); 

    it(`should have as title 'app works!'`, async(() => { 
    const fixture = TestBed.createComponent(AppComponent); 
    const app = fixture.debugElement.componentInstance; 
    expect(app.title).toEqual('app works!'); 
    })); 

}); 

雖然有很多的如何解決在SO服務(特別是路由)的討論中,我沒有發現任何東西非常簡單的類,像警報文件如下:

export class Alert {  
    public static id: number = 0; 
    public message: string; 
    public id: number; 
    public type: string; 
    public dismissible: boolean; 
    public dismissOnTimeout: number; 

    constructor(config: any) { 
     this.message = config.message; 
     this.id = Alert.id && Alert.id++; 
     this.type = config.type || 'info'; 
     this.dismissible = true; 
     this.dismissOnTimeout = settings.alertDismissOnTimeout;   
    }   
} 

這個「模型」文件沒有任何方法和僅作爲,你可以用「新的警報(警報)」實例警報的方便定義存在。它由AlertsService使用,它似乎到目前爲止工作,appComponent的HTML引用AlertsService和實例化Alert對象的一些方法。到目前爲止,這麼好,但是當我嘗試編譯AppComponent時,我得到了標題中的錯誤。我試過使用Alert類的一個存根,簡化到它的絕對最小值(基本上是一個空構造函數),並且返回相同的錯誤。

我(可能不正確)的理解是,我不知何故必須將參數傳遞給警報對象,以便測試套件正常工作。我相當肯定,由於代碼的其他部分是結構化的,並且實際的應用程序運行沒有問題,所以這不是循環依賴問題。這裏的問題是我是否需要傳遞某種模擬參數,如果不是,我該怎麼做才能使該規範正確編譯?

回答

0

我能夠通過從@ angular/core導入NO_ERRORS_SCHEMA並將其作爲模式提供給configureTestingModule()塊來解決我的問題。由於此測試的目的不是處理警報或遠遠超過「Hello World」類型的初始化,實際上注入了大部分與警報相關的代碼是不必要的,並且通過將事物分離開來,我能夠得到這個版本編譯:

import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core'; 
// We need NO_ERRORS_SCHEMA to ignore subcomponents we aren't actually testing, like the alert modules. 
import { TestBed, async } from '@angular/core/testing'; 
import { AppComponent } from './app.component'; 
import { AlertsService } from './services/alerts.service'; 
import { AboutComponent } from './about/about.component'; 
import * as ng2Bootstrap from 'ng2-bootstrap'; 

describe('AppComponent',() => { 
    beforeEach(() => { 

    TestBed.configureTestingModule({ 
     declarations: [ 
     AppComponent, AboutComponent 
     ], 
     imports: [ 
     ng2Bootstrap.Ng2BootstrapModule 
     ], 
     providers: [ 
     AlertsService, 
     ], 
     schemas: [NO_ERRORS_SCHEMA] 
    }); 
    TestBed.compileComponents(); 
    }); 

    it('should create the app', async(() => { 
    const fixture = TestBed.createComponent(AppComponent); 
    const app = fixture.debugElement.componentInstance; 
    expect(app).toBeTruthy(); 
    })); 

    it(`should have 'Application content' as its title`, async(() => { 
    const fixture = TestBed.createComponent(AppComponent); 
    const app = fixture.debugElement.componentInstance; 
    expect(app.title).toEqual('Application content'); 
    })); 

}); 
相關問題