问题
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