【问题标题】:spec has no expextations Jasmine规范没有期望茉莉花
【发布时间】:2020-05-07 16:09:38
【问题描述】:

这是我第一次进行角度测试。我正在使用 Jasmine 和 Karma。所以诀窍是,这个测试实际上通过了,我在覆盖率报告中看到了它。但是测试给了我一个错误,'Spec'AppComponent 应该检查函数 closeMenus()' 没有期望。尽管。我有以下代码:

app.components.ts

import { Component, ViewChild, Directive } from '@angular/core';
import { MatSidenav } from '@angular/material';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  title = 'myApp';

  @ViewChild('sidenav', {static:true}) sidenavbar : MatSidenav;

  openmenu : string = "";

  closeMenus() {
    this.sidenavbar.close();
  }
}

app.component.spec.ts

import { AppComponent } from './app.component';
import { TestBed, async, fakeAsync } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import * as material from '@angular/material';

describe('AppComponent', async() => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule,
        material.MatAutocompleteModule,
        ...
        material.MatTooltipModule,
      ],
      declarations: [
        AppComponent,
      ],
    }).compileComponents();
  }));

  // works but SPEC HAS NO EXPECTATIONS
  it('should check function closeMenus()', (() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    spyOn(app.sidenavbar, 'close');
    app.closeMenus();
    expect(app.sidenavbar.close).toHaveBeenCalled;
  }));
});

我在这里做错了吗?

感谢您的帮助,祝您有愉快的一天:)

【问题讨论】:

    标签: angular jasmine karma-runner karma-coverage


    【解决方案1】:

    如你所愿,你错过了()

    expect(app.sidenavbar.close).toHaveBeenCalled();
    

    【讨论】:

      猜你喜欢
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多