如何窥探茉莉花的价值财产(而不是一种方法)

茉莉花的spyOn是很好的改变一个方法的行为,但有没有办法改变一个对象的价值属性(而不是一种方法)? 代码可能如下所示:

 spyOn(myObj, 'valueA').andReturn(1); expect(myObj.valueA).toBe(1); 

2017年2月,他们合并了一个join这个function的公关,于2017年4月发布。

所以窥探你使用的getter / setter: const spy = spyOnProperty(myObj, 'myGetterName', 'get'); myObj是你的实例,'myGetterName'是在你的类中定义的那个名字, get myGetterName() {} ,第三个参数是typesget或者set

您可以使用与spyOn创build的间谍一样的断言。

所以你可以举个例子:

 const spy = spyOnProperty(myObj, 'myGetterName', 'get'); // to stub and return nothing. Just spy and stub. const spy = spyOnProperty(myObj, 'myGetterName', 'get').and.returnValue(1); // to stub and return 1 or any value as needed. const spy = spyOnProperty(myObj, 'myGetterName', 'get').and.callThrough(); // Call the real thing. 

这里是github源代码中的行,如果你有兴趣,这个方法是可用的。

https://github.com/jasmine/jasmine/blob/7f8f2b5e7a7af70d7f6b629331eb6fe0a7cb9279/src/core/requireInterface.js#L199

回答最初的问题,用茉莉花2.6.1,你会:

 const spy = spyOnProperty(myObj, 'valueA', 'get').andReturn(1); expect(spy).toBe(1); 

茉莉花没有这个function,但是你可能能够使用Object.defineProperty一起破解一些东西。

你可以重构你的代码来使用getter函数,然后窥探getter。

 spyOn(myObj, 'getValueA').andReturn(1); expect(myObj.getValueA()).toBe(1); 

任何原因,你不能直接改变它的对象? 这不是因为如果JavaScript强制对象的属性的可见性。

如果您使用的是ES6(Babel)或TypeScript,则可以使用get和set访问器来存储该属性

 export class SomeClassStub { getValueA = jasmine.createSpy('getValueA'); setValueA = jasmine.createSpy('setValueA'); get valueA() { return this.getValueA(); } set valueA(value) { this.setValueA(value); } } 

然后在你的testing中,你可以检查该属性设置为:

 stub.valueA = 'foo'; expect(stub.setValueA).toHaveBeenCalledWith('foo'); 

假设有这样的方法需要testing小图像的src属性需要检查

 function reportABCEvent(cat, type, val) { var i1 = new Image(1, 1); var link = getABC('creosote'); link += "&category=" + String(cat); link += "&event_type=" + String(type); link += "&event_value=" + String(val); i1.src = link; } 

下面的spyOn()会导致“新的图像”被从testing中提供的伪代码spyOn代码返回一个只有src属性的对象

由于variables“钩子”的范围是在SpyOn中的伪代码中可见的,以及之后在调用“reportABCEvent”之后

 describe("Alphabetic.ads", function() { it("ABC events create an image request", function() { var hook={}; spyOn(window, 'Image').andCallFake( function(x,y) { hook={ src: {} } return hook; } ); reportABCEvent('testa', 'testb', 'testc'); expect(hook.src). toEqual('[zubzub]&arg1=testa&arg2=testb&event_value=testc'); }); 

这是为茉莉花1.3,但可能工作在2.0,如果“andCallFake”被更改为2.0的名称

我正在使用kendo网格,因此不能将实现更改为getter方法,但是我想testing一下(嘲讽网格)而不是testing网格本身。 我正在使用间谍对象,但这不支持属性嘲笑,所以我这样做:

  this.$scope.ticketsGrid = { showColumn: jasmine.createSpy('showColumn'), hideColumn: jasmine.createSpy('hideColumn'), select: jasmine.createSpy('select'), dataItem: jasmine.createSpy('dataItem'), _data: [] } 

这有点冗长,但它是一种享受

我在这里知道的晚会有点迟,但是,

你可以直接访问调用对象,它可以给你每个调用的variables

 expect(spy.calls.argsFor(0)[0].value).toBe(expectedValue) 

最好的方法是使用spyOnProperty 。 它期望3个属性,你需要通过getset为第三个属性。

 const div = fixture.debugElement.query(By.css('.ellipsis-overflow')); // now mock properties spyOnProperty(div.nativeElement, 'clientWidth', 'get').and.returnValue(1400); spyOnProperty(div.nativeElement, 'scrollWidth', 'get').and.returnValue(2400); 

在这里,我设置了div.nativeElement对象的clientWidthdiv.nativeElement