2017-06-01 160 views
4

我剛剛開始使用Karma第一次...在此tutuorial:https://angular.io/docs/ts/latest/guide/testing.html我正在寫簡單的測試,以檢查標題是否正確。我總是得到這個錯誤:「沒有捕獲瀏覽器,打開http://localhost:9876/。我正在使用Angular 2和打字稿。這些版本Karma錯誤 - 沒有捕獲的瀏覽器,打開http:// localhost:9876/

"@angular/core": "2.4.10" 
"jasmine-core": "^2.6.2", 
"karma": "^1.7.0". 

我的文件夾結構看起來像這樣

mydashboard 
-src 
    -app 
    -welcome 
     -welcome.component.ts 
     -welcome.component.spec.ts 
-karma.conf.js 

//karma.conf.js 
module.exports = function(config) { 
    config.set({ 
    basePath: '', 
    frameworks: ['jasmine'], 
    files: ["src/app/**/*.spec.ts" 
    ], 
    exclude: [ 
    ], 
    preprocessors: { 
    }, 
    reporters: ['progress'], 
    port: 9876, 
    colors: true, 
    logLevel: config.LOG_INFO, 
    autoWatch: true, 
    browsers: ['Chrome'], 
    singleRun: false, 
    concurrency: Infinity 
    }) 
} 
//welcome.component.spec.ts 
import { ComponentFixture, TestBed } from '@angular/core/testing'; 
import { By }    from '@angular/platform-browser'; 
import { DebugElement } from '@angular/core'; 
import { WelcomeComponent } from './welcome.component'; 

describe('WelcomeComponent (inline template)',() => { 
    let comp: WelcomeComponent; 
    let fixture: ComponentFixture<WelcomeComponent>; 
    let de:  DebugElement; 
    let el:  HTMLElement; 
    beforeEach(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ WelcomeComponent ], // declare the test component 
    }); 
    fixture = TestBed.createComponent(WelcomeComponent); 
    comp = fixture.componentInstance; // WelcomeComponent test instance 
    // query for the title <h1> by CSS element selector 
    de = fixture.debugElement.query(By.css('h1')); 
    el = de.nativeElement; 
    }); 

    it('should display original title',() => { 
    fixture.detectChanges(); 
    expect(el.textContent).toContain(comp.title); 
}); 
}); 
//welcome.component.ts 
import { Component } from '@angular/core'; 
@Component({ 
    template: '<h1>{{title}}</h1>' 
}) 
export class WelcomeComponent { 
    title = 'Test Tour of Heroes'; 
} 

enter image description here

+1

這只是一個警告(加上鉻開始後),你的問題在別的地方,你能提供更多的意見嗎?你的配置中沒有'plugins'部分。像'plugins:['karma-jasmine','karma-chrome-launcher']'。請將logLevel改爲config.LOG_DEBUG – Edwin

+0

@Edwin謝謝。我添加了插件並更改了logLevel。我得到相同的警告和錯誤'執行了0的0錯誤(0.023秒/ 0秒)'。我已經在上面添加了其他代碼示例。 welcome.component.spec.ts和welcome.component.ts – Mythri

+0

,現在使用新的日誌級別,你會得到新的輸出嗎?或者是一樣的?然後是問題:你有'node_modules'文件夾中的這些插件嗎? – Edwin

回答

4

與後來的expect(...)調用,導致茉莉花的代碼看似無限循環fixture.debugElement.query一些衝突調用。

例如,當匹配#my-id對象存在以下原因會導致錯誤:

expect(fixture.debugElement.query(By.css('#my-id'))).toBeFalsy(); 

你的情況,你有來電的不同組合,但它是相同的配方:query加上一些expect電話。

作爲臨時解決辦法,我們可以使用queryAll(...).length代替:

expect(fixture.debugElement.queryAll(By.css('#my-id')).length).toBeFalsy(); 

這是茉莉花的錯誤,並在這些頁面已經報道:

相關問題