2016-11-19 33 views
1

spyOn(service,someMethod).and.callThrough();茉莉spyOn不考虑角crubbins

然后...

var an_object = some_objects[0]; 
useADirectiveThatRepeatsOver(some_objects); 

expect(service.someMethod).toHaveBeenCalledWith(an_object); 

失败

Expected spy someMethod to have been called with [ Object({ 
    sent_date: '2016-11-18T12:06:29.712318Z', 
    email: '[email protected]', url: '/api/v1/invites/1/' }) ] 
    but actual calls were [ Object({ 
    sent_date: '2016-11-18T12:06:29.712318Z', 
    email: '[email protected]', url: '/api/v1/invites/1/', 
    $$hashKey: 'object:110' }) ]. 

的指令:

<div ng-repeat="obj in some_objects"> 
    <span ng-click="someControllerMethod(obj)">{{ obj.email }}</span> 
</div> 

问题,AFAICT,发生在someControllerMethod(OBJ)被称作 - 外该方法(例如就在obj没有$$ hashkey之前,但是一旦调用someControllerMethod(),obj就已经获得了错误的关键。

我假设没有办法防止角度做它做什么,那么有什么办法可以缓解这种情况?显然,我不能使用angular.equals()而不是toEqual(),因为toHaveBeenCalledWith()方法没有公开方法来改变比较器。

回答

0

原来这可以通过以下方式解决:

<div ng-repeat="obj in some_objects track by field">...</div> 

所以对于我的具体情况:

<div ng-repeat="invite in invites track by email">...</div>