2017-04-14 31 views
0

我在angular-cli項目上使用ngx-bootstrap/datepicker創建了獨立組件。 一切正常,但單元測試失敗。 但我是單元測試新手,我試圖測試,但它說失敗。如何在實現ngx-bootstrap/datepicker之後通過單元測試?

這是Travic-CI構建日誌。

https://travis-ci.org/webcat12345/webcat-black-page/builds/221961698

這裏是我的項目版本和代碼。

@angular/cli: 1.0.0 
node: 7.6.0 
os: linux x64 
@angular/cli: 1.0.0 
@angular/common: 4.0.2 
@angular/compiler: 4.0.2 
@angular/compiler-cli: 4.0.2 
@angular/core: 4.0.2 
@angular/forms: 4.0.2 
@angular/http: 4.0.2 
@angular/platform-browser: 4.0.2 
@angular/platform-browser-dynamic: 4.0.2 
@angular/router: 4.0.2 

模板

<datepicker class="well well-sm main-calendar" [(ngModel)]="dt" [minDate]="minDate" [showWeeks]="false" [dateDisabled]="dateDisabled"></datepicker> 

組件(抱歉發佈完整的代碼,它是從NGX-引導演示只是示例代碼)

import { Component, OnInit } from '@angular/core'; 
import * as moment from 'moment'; 

@Component({ 
    selector: 'app-sidebar-datepicker', 
    templateUrl: './sidebar-datepicker.component.html', 
    styleUrls: ['./sidebar-datepicker.component.scss'] 
}) 
export class SidebarDatepickerComponent implements OnInit { 

    public dt: Date = new Date(); 
    public minDate: Date = void 0; 
    public events: any[]; 
    public tomorrow: Date; 
    public afterTomorrow: Date; 
    public dateDisabled: {date: Date, mode: string}[]; 
    public formats: string[] = ['DD-MM-YYYY', 'YYYY/MM/DD', 'DD.MM.YYYY', 
    'shortDate']; 
    public format: string = this.formats[0]; 
    public dateOptions: any = { 
    formatYear: 'YY', 
    startingDay: 1 
    }; 
    private opened: boolean = false; 

    constructor() { 
    (this.tomorrow = new Date()).setDate(this.tomorrow.getDate() + 1); 
    (this.afterTomorrow = new Date()).setDate(this.tomorrow.getDate() + 2); 
    (this.minDate = new Date()).setDate(this.minDate.getDate() - 1000); 
    (this.dateDisabled = []); 
    this.events = [ 
     {date: this.tomorrow, status: 'full'}, 
     {date: this.afterTomorrow, status: 'partially'} 
    ]; 
    } 

    ngOnInit() { 

    } 

    public getDate(): number { 
    return this.dt && this.dt.getTime() || new Date().getTime(); 
    } 

    public today(): void { 
    this.dt = new Date(); 
    } 

    public d20090824(): void { 
    this.dt = moment('2009-08-24', 'YYYY-MM-DD') 
     .toDate(); 
    } 

    public disableTomorrow(): void { 
    this.dateDisabled = [{date: this.tomorrow, mode: 'day'}]; 
    } 

    // todo: implement custom class cases 
    public getDayClass(date: any, mode: string): string { 
    if (mode === 'day') { 
     let dayToCheck = new Date(date).setHours(0, 0, 0, 0); 

     for (let event of this.events) { 
     let currentDay = new Date(event.date).setHours(0, 0, 0, 0); 

     if (dayToCheck === currentDay) { 
      return event.status; 
     } 
     } 
    } 

    return ''; 
    } 

    public disabled(date: Date, mode: string): boolean { 
    return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6)); 
    } 

    public open(): void { 
    this.opened = !this.opened; 
    } 

    public clear(): void { 
    this.dt = void 0; 
    this.dateDisabled = undefined; 
    } 

    public toggleMin(): void { 
    this.dt = new Date(this.minDate.valueOf()); 
    } 
} 

測試代碼

import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; 
import { DatepickerModule } from 'ngx-bootstrap/datepicker'; 
import { SidebarDatepickerComponent } from './sidebar-datepicker.component'; 

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

    beforeEach(async(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ SidebarDatepickerComponent ], 
     schemas: [CUSTOM_ELEMENTS_SCHEMA], 
     imports: [DatepickerModule.forRoot()] 
    }) 
    .compileComponents(); 
    })); 

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

    it('should create',() => { 
    expect(component).toBeTruthy(); 
    }); 
}); 

請幫我弄清楚這個問題。

謝謝!

回答

3

1)使用NO_ERRORS_SCHEMA,而不是CUSTOM_ELEMENTS_SCHEMA因爲:

CUSTOM_ELEMENTS_SCHEMA將使:

  • 在他們的名字-任何非角元素,
  • 任何性質的元素在一個-他們的名字是定製的常用規則

但你的組件沒有-datepicker

NO_ERRORS_SCHEMA允許任何財產上的元素

TestBed.configureTestingModule({ 
    declarations: [ SidebarDatepickerComponent ], 
    schemas: [NO_ERRORS_SCHEMA], 
    imports: [DatepickerModule.forRoot()] 
}) 

2)另一種選擇是進口FormsModule

TestBed.configureTestingModule({ 
    declarations: [ SidebarDatepickerComponent ], 
    imports: [DatepickerModule.forRoot(), FormsModule] 
}) 
+0

這是作品的魅力!我用第二個選項。還有一個問題,哪一個更適合於實踐?謝謝。 – blackiii

+0

我認爲這取決於你的架構。如果您知道使用NO_ERROR_SCHEMA會更好,因爲您知道所有測試組件,並且已經聲明並導入了所需的所有內容,請使用此選項。我更願意控制我所有的依賴關係,並且不使用任何模式。也請查閱這篇偉大的文章https://blog.nrwl.io/essential-angular-testing-192315f8be9b – yurzui

+0

非常感謝您的幫助! – blackiii

相關問題