Jasmine 测试在 angular.copy 上添加未定义的前缀

Jasmine test prepends undefined on angular.copy

我一直在使用 Jasmine 单元测试来测试 Angularjs 前端,但出现了一个奇怪的错误:

Expected doStuff to have been called with [ Object({ foo: 'bar' }) ]

but actual calls were [ undefined, Object({ foo: 'bar' }) ]

单元测试代码:

it('TestCase - test angular broadcast', function() {
    controller.anObjectInController = { foo: 'bar' };
    scope.$broadcast('doStuffBroadcast');

    expect(myService.doStuff).toHaveBeenCalledWith({ foo: 'bar' });
});

正在测试的实际代码:

$scope.$on('doStuffBroadcast', function() {
    var fields = angular.copy(vm.anObjectInController);
    vm.doStuff(fields);
});

为什么 angular 会在简单的 angular.copy 上添加 undefined ?为什么它仍然显示为数组?

现在,只是回答之前的评论,测试不是谎言(不像蛋糕)。

因为我的部分代码是在稍后阶段从外部存储库加载的,所以无法控制它。因此,问题的解决方案是创建一个 myExtraJQueryLibraryCodeService 用方法 doStuff(element)element.doStuff(element)

调用库后它工作得很好,myExtraJQueryLibraryCodeService 是唯一可以从单元测试中跳过的地方,因为我们不能让这个外部库工作。