简体   繁体   English

Jasmine 间谍永远不会被调用

[英]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.

相关问题 jasmine 监视 window.innerwidth 没有被调用 - jasmine spy on window.innerwidth not getting called Jasmine+Karma:ActivatedRoute 间谍没有被调用 - Jasmine+Karma: ActivatedRoute spy not not getting called Angular Jasmine 间谍没有被调用 - Angular Jasmine spy not being called 在测试组件中的主题上调用 next() 时,不会调用 Angular Jasmine 间谍 - Angular Jasmine spy is not getting called when calling the next() on a subject in the test component 预期的间谍导航已使用[[&#39;&#39;ProjectData / MasterSequence&#39;]]调用,但从未调用过 - Expected spy navigate to have been called with [ [ '/ProjectData/MasterSequence' ] ] but it was never called 预期的间谍导航已使用 [ [ '/login' ] ] 调用,但从未调用过 - Expected spy navigate to have been called with [ [ '/login' ] ] but it was never called 预期的间谍导航已使用 ['accounts'] 调用,但从未调用过 - Expected spy navigate to have been called with [ 'accounts' ] but it was never called Angular Jasmine预期的间谍onButtonClick已被调用 - Angular Jasmine Expected spy onButtonClick to have been called 收到错误消息“预期的间谍 <functionName> 被称为“ 6角茉莉花” - Get an Error “Expected spy <functionName> to have been called” in angular 6 jasmine 使用 Jasmine 进行 Angular 测试“预期的间谍已被调用。” - Angular Testing With Jasmine "Expected spy to have been called."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM