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

2023-11-27

我刚刚开始第一次使用 Karma...遵循本教程:https://angular.io/docs/ts/latest/guide/testing.html我正在编写简单的测试来检查标题是否正确。我总是明白这个错误:“没有捕获的浏览器,请打开http://本地主机:9876/".我正在使用 Angular 2 和 typescript。这些是版本

"@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


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

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

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

在您的情况下,您有不同的调用组合,但它是相同的配方:query加一些expect calls.

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

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

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

  • 这里是为了茉莉花
  • 这里是角度
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐