Declaring getter property when building SpyObj using jasmine.createSpyObj utility?

本小妞迷上赌 提交于 2019-12-10 17:09:44

问题


Let's say I have a class:

class MyRealClass {
  get propOne() { return stuffFromTheServer; }
}

When testing, I want to achieve this functionality:

const mockClass = {
  get propOne() { return someStuff; }
}

jasmine.spyOnProperty(mockClass, 'propOne', 'get');

By doing something like this...

const spy = jasmine.createSpyObj('mockClass', [
  {methodName: 'propOne', accessType: 'get'}
]);

In other words, I want to build a SpyObj<MyRealClass> using the jasmine.createSpyObj and declare the getter properties as methods in the methodName array (the second parameter the the createSpyObj() method.

Is this possible?


回答1:


I did it surprisingly simple by this code:

const routerMock = jasmine.createSpyObj(['events']);
routerMock.events = of(new NavigationEnd(0, 'url1', 'url2'));

const serviceToTest = new SomeService(routerMock);


来源:https://stackoverflow.com/questions/49135532/declaring-getter-property-when-building-spyobj-using-jasmine-createspyobj-utilit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!