监视函数内部的局部变量

huangapple go评论63阅读模式
英文:

Spy on local variable inside function

问题

I have a method that opens a Dialog and then when closed the dialog, it will delete an element which has been passed.

What i would like to do is to 'spy' on the afterClosed().subscribe method, the method:

deleteDialog = MatDialog;
onDeleteElementClicked($event: any, elementName: string): void {
const dialogRef = this.deleteDialog.open(CustomDialog, {
data: {
//SOME DATA
});

dialogRef.afterClosed().subscribe(result => {
    this.toggleFlagAndCall(result, () => {
        this.deleteElement(this.result.name);
    });
});

}

The test:

describe('DeleteComponent', () => {
let component: DeleteComponent;
let fixture: ComponentFixture;

beforeEach(async () => {
    await TestBed.configureTestingModule({
        declarations: [
            DeleteComponent,
        ],
        imports: [
            // some imports
        ],
        providers: [
            { provide: MatDialogRef, useClass: MatDialogRef, useValue: { afterOpen: () => of(true)}},
            { provide: DeleteDialogComponent, useValue: { afterClosed: () => of(true)}},
            { provide: MatDialog, useValue: { afterClosed: () => of(true),
                open: () => of(true)}},
            }
        ]
    })
        .compileComponents();
});

beforeEach(() => {
    fixture = TestBed.createComponent(DeleteComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
});

it('should create', () => {
    expect component).toBeTruthy();
});

it('should delete a single element', () => {
    const event = {
        preventDefault: () => {},
        stopPropagation: () => {}
    };

    component.onDeleteElementClicked(event, 'toBeDeletedElement');
    expect(component.elements.length).toBe(0);
});

});

I get the error:

TypeError: dialogRef.afterClosed is not a function
But i thought that this line:

{ provide: MatDialog, useValue: { afterClosed: () => of(true),

Would create the mock method for 'afterclosed' and would return an observable, but it does not even get invoked.
How can i spy on dialogRef.afterClosed().?

英文:

I have a method that opens a Dialog and then when closed the dialog, it will delete an element which has been passed.<br>
What i would like to do is to 'spy' on the afterClosed().subscribe method, the method:

deleteDialog = MatDialog;
onDeleteElementClicked($event: any, elementName: string): void {
        const dialogRef = this.deleteDialog.open(CustomDialog, {
            data: {
                //SOME DATA
           });

 
        dialogRef.afterClosed().subscribe(result =&gt; {
            this.toggleFlagAndCall(result, () =&gt; {
                this.deleteElement(this.result.name);
            });
        });
    }

The test:

describe(&#39;DeleteComponent&#39;, () =&gt; {
    let component: DeleteComponent;
    let fixture: ComponentFixture&lt;DeleteComponent&gt;;

    beforeEach(async () =&gt; {
        await TestBed.configureTestingModule({
            declarations: [
                DeleteComponent,
            ],
            imports: [
                // some imports
            ],
            providers: [
                { provide: MatDialogRef, useClass: MatDialogRef, useValue: { afterOpen: () =&gt; of(true)}},
                { provide: DeleteDialogComponent, useValue: { afterClosed: () =&gt; of(true)}},
                { provide: MatDialog, useValue: { afterClosed: () =&gt; of(true),
                    open: () =&gt; of(true)}},
                }
            ]
        })
            .compileComponents();
    });

    beforeEach(() =&gt; {
        fixture = TestBed.createComponent(DeleteComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it(&#39;should create&#39;, () =&gt; {
        expect(component).toBeTruthy();
    });

    it(&#39;should delete a single element&#39;, () =&gt; {
        const event = {
            preventDefault: () =&gt; {},
            stopPropagation: () =&gt; {}
        };

        component.onDeleteElementClicked(event, &#39;toBeDeletedElement&#39;);
        expect(component.elements.length).toBe(0);
    });
});

I get the error:

TypeError: dialogRef.afterClosed is not a function

But i thought that this line:

{ provide: MatDialog, useValue: { afterClosed: () => of(true),

Would create the mock method for 'afterclosed' and would return an observable, but it does not even get invoked.<br>
How can i spy on dialogRef.afterClosed().?

答案1

得分: 1

你可以创建一个模拟类 MatDialog

class MatDialogMock {
  open() {
    return {
      afterClosed: () => of(true)
    };
  }
}

然后在测试中,你可以使用这个模拟类

providers: [
  { provide: MatDialog, useClass: MatDialogMock }
]

现在,当你的组件使用 MatDialog 时,你会得到一个 MatDialogMock 的实例,然后你可以使用 afterClosed 属性来进行监视。

英文:

You could make a mock class MatDialog

class MatDialogMock {
  open() {
    return {
      afterClosed: () =&gt; of(true)
    };
  }
}

Then in the test, you can use that mock class

providers: [
  { provide: MatDialog, useClass: MatDialogMock }
]

You now get an instance of MatDialogMock when your component uses MatDialog, and you can then use the afterClosed property to spy on it.

huangapple
  • 本文由 发表于 2023年2月18日 17:57:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定