2016-09-21 175 views
3

我試圖執行相對獨立的Pipe測試。我正在使用angular-cli(與@angular 2.0.0)的最新版本。角度2 - 角度cli管道測試與依賴關係

管代碼是:

import { Pipe, PipeTransform } from "@angular/core"; 
import { DatePipe, JsonPipe } from "@angular/common"; 

@Pipe({name: 'dataTableFormat'}) 
export class DataTablePipe implements PipeTransform { 

    // values with type 'json' are parsed to json. As a result, string values may be displayed with quotes ("<string>"). 
    // To avoid that, we remove these quotes with this regex 
    private quotesExp: RegExp = /^\"|\"$/gi; 

    constructor(private datePipe: DatePipe, private jsonPipe: JsonPipe) { 
    } 

    transform(value: string, type: string): string { 
    switch (type) { 
     case "date": 
      return this.datePipe.transform(value, 'short'); 
     case "json": 
      return this.jsonPipe.transform(value).replace(this.quotesExp, ""); 
     default: 
      return value; 
    } 
    } 
} 

和規範代碼是:

import {TestBed} from "@angular/core/testing"; 
import {DataTablePipe} from "./data-table.pipe"; 
import {DatePipe, JsonPipe} from "@angular/common"; 

describe('DataTableFormat',() => { 

    beforeEach(() => { 
     TestBed.configureTestingModule({ 
      declarations: [ DataTablePipe ], 
      providers: [ 
       DatePipe, JsonPipe 
      ] 
     }); 
    }); 

    it('sanity',() => { 
     expect(true).toBeTruthy(); 
    }); 

    it('should transform ',() => { 
     let fixture = TestBed.createComponent(DataTablePipe); 
     let comp = fixture.componentInstance; 
     let testDate:Date = new Date(); 
     let datePipe = fixture.debugElement.injector.get(DatePipe); 

     expect(comp.transform(testDate.toString(), 'date')).toBe(datePipe.transform(testDate)); 

    }); 
}); 

神智測試通過,但真正的測試失敗,錯誤:

Error: Cannot create the component DataTablePipe as it was not imported into the testing module! 
    at TestBed.createComponent (webpack:///Users/sninio/dev/csp-ui-ng/~/@angular/core/bundles/core-testing.umd.js:1144:0 <- src/main/js/test.ts:6022:23) 
    at Function.TestBed.createComponent (webpack:///Users/sninio/dev/csp-ui-ng/~/@angular/core/bundles/core-testing.umd.js:972:0 <- src/main/js/test.ts:5850:33) 
    at Object.<anonymous> (webpack:///Users/sninio/dev/csp-ui-ng/src/main/js/app/pages/+platform/events/data-table/data-table.pipe.spec.ts:23:30 <- src/main/js/test.ts:14770:41) 
    at ZoneDelegate.invoke (webpack:///Users/sninio/dev/csp-ui-ng/~/zone.js/dist/zone.js:203:0 <- src/main/js/test.ts:26741:28) 
    at ProxyZoneSpec.onInvoke (webpack:///Users/sninio/dev/csp-ui-ng/~/zone.js/dist/proxy.js:72:0 <- src/main/js/test.ts:18285:39) 
    at ZoneDelegate.invoke (webpack:///Users/sninio/dev/csp-ui-ng/~/zone.js/dist/zone.js:202:0 <- src/main/js/test.ts:26740:34) 
    at Zone.run (webpack:///Users/sninio/dev/csp-ui-ng/~/zone.js/dist/zone.js:96:0 <- src/main/js/test.ts:26634:43) 
    at Object.<anonymous> (webpack:///Users/sninio/dev/csp-ui-ng/~/zone.js/dist/jasmine-patch.js:91:27 <- src/main/js/test.ts:18021:50) 

我肯定是我錯過了一些配置,但由於某種原因,DataTablePipe沒有導入到第e測試模塊...

任何想法?

回答

6

TestBed.createComponent嚴格用於創建組件。你可以做的是將DatePipeJsonPipe注入測試。剛剛創建你從這些管道

import { inject } from '@angular/core/testing'; 

it('should transform ', inject([DatePipe, JsonPipe], (datePipe, jsonPipe) => { 
    let pipe = new DataTablePipe(datePipe, jsonPipe); 
    let testDate: Date = new Date(); 

    expect(pipe.transform(testDate.toString(), 'date')) 
     .toBe(datePipe.transform(testDate, 'short')); 
})); 
+0

這不起作用,我得到一個缺少提供者錯誤:錯誤:沒有DatePipe提供程序!在src/main/js/test.ts中 –

+1

當我測試時,對我來說工作得很好。我使用了您發佈的確切代碼。我剛剛從'TestBed'中移除了'declarations'。除此之外,一切都是一樣的,除了我在回答中發佈的內容 –

+0

您是否使用angular-cli測試了它?我相信它是我缺少的一些配置 –