I set up a Jasmine spy to test that when the user clicks a button (triggering the goBack() function), it opens a dialog component, or as far as the test is concerned, calls a thing. It's a really simple test, and I have virtually identical tests that work, but this one doesn't for some reason. The output of the test says Expected spy open to have been called
.
component-with-a-button.component.ts
import { MatDialog } from '@angular/material/dialog';
export class ComponentWithAButton {
constructor(private closeDialog: MatDialog) {}
// A button triggers the following function
goBack(): void {
this.closeDialog.open(CloseDialogComponent);
}
}
component-with-a-button.component.spec.ts
import { MatDialog } from '@angular/material/dialog';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentWithAButton } from './component-with-a-button.component';
const closeDialogMock = { open: () => {} };
describe('ComponentWithAButton', () => {
let component: ComponentWithAButton;
let fixture: ComponentFixture<ComponentWithAButton>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ComponentWithAButton],
providers: [
{
provide: MatDialog,
useValue: closeDialogMock,
}
],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ComponentWithAButton);
component = fixture.componentInstance;
fixture.detectChanges();
});
describe('goBack', () => {
it('should call closeDialog.open', () => {
const closeDialogSpy = spyOn(closeDialogMock, 'open');
component.goBack();
expect(closeDialogSpy).toHaveBeenCalled();
});
});
});