[英]Jasmine spy never getting called
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.我设置了一个 Jasmine 间谍来测试当用户单击一个按钮(触发 goBack() 函数)时,它会打开一个对话框组件,或者就测试而言,调用一个东西。 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
.测试的 output 显示
Expected spy open to have been called
。
component-with-a-button.component.ts带按钮的组件.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带按钮的组件.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();
});
});
});
As it turns out, jasmine won't let you use two providers of the same type (well, theoretically you should be able to add the multi: true
flag to use multiple of the same provider, but it wasn't working for me, so that's something I need to do more reading on), and the final one will take precedence.事实证明, jasmine 不会让你使用两个相同类型的提供者(好吧,理论上你应该能够添加
multi: true
标志来使用多个相同的提供者,但它对我不起作用,所以这是我需要做更多阅读的事情),最后一个将优先。 In the question, I simplified my actual code to focus on where I thought the problem lay.在这个问题中,我简化了我的实际代码以专注于我认为问题所在的位置。 My actual component code looked more like this (notice two
MatDialog
s instead of one):我的实际组件代码看起来更像这样(注意两个
MatDialog
而不是一个):
import { MatDialog } from '@angular/material/dialog';
import { CloseDialogComponent } from 'some/direcotry/close-dialog.component';
import { SubmitDialogComponent } from '/other/directory/submit-dialog.component';
export class ComponentWithAButton {
constructor(private closeDialog: MatDialog, private submitDialog: MatDialog) {}
goBack(): void {
this.closeDialog.open(CloseDialogComponent);
}
submit(): void {
this.submitDialog.open(SubmitDialogComponent);
}
}
To resolve the issue of having multiple providers of one type, I reduced the MatDialog
s in the constructor to one and wrote a dialog builder function that takes a generic component, so now the component looks more like this:为了解决一种类型的多个提供者的问题,我将构造函数中的
MatDialog
减少为一个,并编写了一个对话框构建器 function ,它采用通用组件,所以现在组件看起来更像这样:
import { MatDialog } from '@angular/material/dialog';
import { ComponentType } from '@angular/cdk/portal';
import { CloseDialogComponent } from 'some/direcotry/close-dialog.component';
import { SubmitDialogComponent } from '/other/directory/submit-dialog.component';
export class ComponentWithAButton {
constructor(private dialogConstructor: MatDialog) {}
goBack(): void {
this.constructDialog(CloseDialogComponent);
}
submit(): void {
this.constructDialog(SubmitDialogComponent);
}
private constructDialog(component: ComponentType<any>) {
this.dialogConstructor.open(component);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.