Search code examples
angularjasminekarma-runnerkarma-coverage

spec has no expextations Jasmine


It's my first experience with angular testing. I am using Jasmine and Karma for it. So trick is, that this test actually passes and i see it in coverage report. But test gives me an error with 'Spec 'AppComponent should check function closeMenus()' has no expectations.' though. I have following code:

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;
  }));
});

Am I doing anything wrong here?

Thank you for your help and have a nice day :)


Solution

  • You are missing () at your expectation:

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