测试带有匿名函数内部匿名功能的ADDListener -Angular Jasmine(隔离测试)
最近有人帮助我进行了功能测试,但是我无法访问该功能的一部分,因为它是AddListener中的匿名功能。我阅读了文档,但我找不到任何内容。可以访问这些区域吗?
这是我的组件代码
import { EventEmitter } from "events";
@Component({
selector: "app-mug",
templateUrl: "./mug.component.html",
styleUrls: ["./mug.component.scss"],
})
export class MugComponent implements OnInit
{
@Output() nav: EventEmitter<number> = new EventEmitter<number>();
public mug: Mug= new Mug();
constructor(
public constants: Constants,
public mugService: mugService,
public basicService: BasicService,
public toolsService: ToolsService,
private modalService: NgbModal,
)
ngOnInit() {
this.basicService.siteChange.subscribe((value) => {
this.mugService.filterC.id = value;
this.mugService.getRelatedMugs();
});
}
public openModal(contentType: string): void {
this.toolsService.eventEmitter.removeAllListeners();
this.toolsService.eventEmitter.addListener("close-modal", () =>
this.toolsService.eventEmitter.removeAllListeners()
);
this.toolsService.eventEmitter.addListener(
"contentEvent",
(content) => {
this.setContent(contentType, content);
}
);
const data = {
component: ModalSearchComponent,
input: inputList,
ModalTitle: "Mug finder",
};
const modalC = this.modalService.open(ModalContainerComponent, {
size: "lg",
backdrop: "static",
});
modalC.componentInstance.showComponent(data);
}
}
let modalService = jasmine.createSpyObj("modalService", ["open"]);
modalService.open.and.returnValue({
componenntInstance: {
showComponent: () => null,
}
});
let toolsService = jasmine.createSpyObj("toolsService", ["toRoute"], {
eventEmitter: {
addListener: () => null,
removeAllListeners: () => null,
}
});
....
component = new MugComponent(
constants,
mugService,
basicService,
toolsService,
modalService,
);
这是我的规格。
.net/wwa4l.png“ rel =“ nofollow noreferrer”>
Someone here recently helped me with a function test, but I can't access a part of that function because it's an anonymous function inside an addListener.I read the documentation but I can't find anything about this. Is it possible to access these areas?
This is my component code
import { EventEmitter } from "events";
@Component({
selector: "app-mug",
templateUrl: "./mug.component.html",
styleUrls: ["./mug.component.scss"],
})
export class MugComponent implements OnInit
{
@Output() nav: EventEmitter<number> = new EventEmitter<number>();
public mug: Mug= new Mug();
constructor(
public constants: Constants,
public mugService: mugService,
public basicService: BasicService,
public toolsService: ToolsService,
private modalService: NgbModal,
)
ngOnInit() {
this.basicService.siteChange.subscribe((value) => {
this.mugService.filterC.id = value;
this.mugService.getRelatedMugs();
});
}
public openModal(contentType: string): void {
this.toolsService.eventEmitter.removeAllListeners();
this.toolsService.eventEmitter.addListener("close-modal", () =>
this.toolsService.eventEmitter.removeAllListeners()
);
this.toolsService.eventEmitter.addListener(
"contentEvent",
(content) => {
this.setContent(contentType, content);
}
);
const data = {
component: ModalSearchComponent,
input: inputList,
ModalTitle: "Mug finder",
};
const modalC = this.modalService.open(ModalContainerComponent, {
size: "lg",
backdrop: "static",
});
modalC.componentInstance.showComponent(data);
}
}
This is my Spec.ts code
let modalService = jasmine.createSpyObj("modalService", ["open"]);
modalService.open.and.returnValue({
componenntInstance: {
showComponent: () => null,
}
});
let toolsService = jasmine.createSpyObj("toolsService", ["toRoute"], {
eventEmitter: {
addListener: () => null,
removeAllListeners: () => null,
}
});
....
component = new MugComponent(
constants,
mugService,
basicService,
toolsService,
modalService,
);
This is my code coverage
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,应该尝试一下:
以上将为您提供最后一次(mostrecent())
addlistener
的调用,但是如果您想对所有调用进行操作,call
我认为应该有所有的电话。Yes, it should be, try this:
The above will give you the last time (mostRecent())
addListener
was called but if you want to have a handle of all of the calls,calls
should have all of the calls I think.