我使用BsModalRef显示模态并使用content
属性发送数据。所以我们有这样的:
this.followerService.getFollowers(this.bsModalRef.content.channelId).subscribe((followers) => {
this.followerList = followers;
this.followerList.forEach((follower) => {
follower.avatarLink = this.setUserImage(follower.userId);
this.followerEmails.push(follower.email);
});
});
我们在bsModalRef(this.bsModalRef.content.channelId
)的内容中设置channelId。它工作正常。现在我正在为此写一个单元测试。问题是我无法嘲笑它。我试过压倒,间谍等,但似乎没有任何作用。我正在使用这个link中提到的方法。一种替代方法是使用TestBed,但我不太了解它的用途。谁能帮助我找到可以实现这一目标的任何方法?
我最近不得不做类似的事情并且模拟方法调用工作。棘手的部分是在测试套件和组件中注入BsModalService。
describe('MyFollowerService', () => {
configureTestSuite(() => {
TestBed.configureTestingModule({
imports: [...],
declarations: [...],
providers: [...]
}).compileComponents();
});
// inject the service
beforeEach(() => {
bsModalService = getTestBed().get(BsModalService);
}
it('test', () => {
// Mock the method call
bsModalService.show = (): BsModalRef => {
return {hide: null, content: {channelId: 123}, setClass: null};
};
// Do the test that calls the modal
});
});
只要您按照以下方式调用bsModal,这种方法就可以了
let bsModalRef = this.modalService.show(MyChannelModalComponent));
最后,这里有一些关于使用TestBed设置测试的更深入的链接。