2017-07-03 44 views
0

我正在開發Angular2 Angular CLI項目,並且正在編寫Jasmine測試用例中的組件,我的項目中依賴於服務的組件是組件構造函數的參數)。當我嘗試編寫組件測試時,得到無法解析NavBarComponent的所有參數:(?,?)Angular2 Jasmine測試用例無法解析NavBarComponent的所有參數:(?,?)

我嘲笑服務,並能夠編寫服務測試。但是,對於組件測試用例,我遇到了上述問題。

知道如何解決這個問題將會很棒。

這裏是我的代碼

export class NavBarComponent implements OnInit { 
     constructor(private sanitizer: DomSanitizer,private service:  AppService){ 

     } 
} 

規格文件

describe('NavBarComponent component ',() => { 

    beforeEach(() => { 
     TestBed.configureTestingModule({ 
      imports: [FormsModule, HttpModule], 
      declarations: [NavBarComponent], 
      providers: [{ provide: Router, useClass: class { navigate = jasmine.createSpy('navigate'); }}, 
      {provide: AppService, useClass: AppService}, 
      {provide: DomSanitizer, useClass: DomSanitizer}], 
      schemas: [NO_ERRORS_SCHEMA] 
     }); 
    }); 


    it('check the number of li elements in NavBarComponent component', 
     async(() => { 
      TestBed 
       .compileComponents() 
       .then(() => { 
        let fixture = TestBed.createComponent(NavBarComponent); 
        let userDOMEl = fixture.debugElement.nativeElement; 
        expect(userDOMEl.querySelectorAll('li').length).toEqual(17); 
       }); 
     })); 


}); 
+0

添加你的構造函數並測試你的嘗試。當你問問題時總是添加含義所需的代碼 – mayur

回答

0

我有一個類似的錯誤,當我tsconfig.json有編譯器選項emitDecoratorMetadata設置爲false,並切換到true解決它。

相關問題