Karma错误 - 没有捕获的浏览器,打开http:// localhost:9876 /

use*_*301 9 javascript karma-runner karma-jasmine angular

我刚刚开始使用Karma第一次...跟随这个tutuorial:https://angular.io/docs/ts/latest/guide/testing.html 我正在编写简单的测试以检查标题是否正确.我总是得到这个错误:"没有捕获的浏览器,打开http:// localhost:9876 / ".我正在使用Angular 2和typescript.这些是版本

"@angular/core": "2.4.10" 
"jasmine-core": "^2.6.2",
"karma": "^1.7.0".
Run Code Online (Sandbox Code Playgroud)

我的文件夹结构如下所示

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';
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Mon*_*Kin 12

一些fixture.debugElement.query与后来的调用冲突的调用expect(...),在Jasmine的代码中导致看似无限的循环.

例如,当匹配的对象#my-id存在时,以下内容将导致错误:

expect(fixture.debugElement.query(By.css('#my-id'))).toBeFalsy();
Run Code Online (Sandbox Code Playgroud)

在你的情况下,你有一个不同的调用组合,但它是相同的食谱:query加上一些expect调用.

作为临时解决方法,我们可以使用queryAll(...).length:

expect(fixture.debugElement.queryAll(By.css('#my-id')).length).toBeFalsy();
Run Code Online (Sandbox Code Playgroud)

这是Jasmine中的一个错误,已在这些页面中报告: