Jasmine spyOn并没有考虑到角形的crubbins

时间:2016-11-19 05:25:05

标签: angularjs object testing jasmine

spyOn(service,someMethod).and.callThrough();

...然后

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: 'pigeon@wacky.races', url: '/api/v1/invites/1/' }) ]
   but actual calls were [ Object({
   sent_date: '2016-11-18T12:06:29.712318Z',
   email: 'pigeon@wacky.races', 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()方法没有公开改变比较器的方法。

1 个答案:

答案 0 :(得分:0)

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

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

所以对于我的具体案例:

$(".textonly").keyup(function (event) {

    var AllowedKeys = [46, 8, 9, 37, 39, 189];
    var textarea = this;

    var kc = event.which || event.keyCode;

    if (!kc || kc == 229) {
        var ss = textarea.selectionStart - 1;
        var ssv = ss || 0;
        var char = textarea.value.substr(ssv, 1);
        var newchar = char.toUpperCase();         
        kc = newchar.charCodeAt(0);
    }

    if ($.inArray(kc, AllowedKeys) !== -1) {}
    else {
        if (kc < 65 || kc > 90) {
            var inputString = $(this).val();
            var shortenedString = inputString.substr(0, (inputString.length - 1));
            $(this).val(shortenedString);             
        }
    }
});