jasmine - TypeError:无法获取未定义或空引用的属性'catch'

时间:2016-02-11 17:04:53

标签: angularjs unit-testing jasmine

我正在尝试在我的方法中为服务调用创建单元测试。单元测试返回以下错误:

TypeError: Unable to get property 'catch' of undefined or null reference

控制器方法我正在测试:

$scope.getAsset = function (id) {
    if ($scope.id != '0') {
        assetFactory.getAsset($scope.id)
        .then(function (response) {
            $scope.asset = response.data;
        })
        .catch(function (error) {
            alertService.add('danger', 'Unable to load asset data: ' + error.statusText + '(' + error.status + '). Please report this error to the application administrator');
        });
    }
};

我的单元测试如下:

it('method getAsset() was called', function () {
    var asset = { AssetId: 'TEST123' };
    var spy = spyOn(assetFactory, 'getAsset').and.callFake(function () {
        return {
            then: function (callback) {
                return callback(asset);
            }
        };
    });
    // call the controller method
    var result = scope.getAsset();
    // assert that it called the service method. must use a spy
    expect(spy).toHaveBeenCalled();
});

当我从我的控制器方法中删除“.catch(function(error)”语句时,测试通过。看来我必须在我的间谍中实现catch,但我无法弄清楚如何。

2 个答案:

答案 0 :(得分:3)

thencatch方法来自于$q服务在AngularJS中实现的promise模式。

你的模拟(伪造)方法也应该返回一个承诺。最简单的方法是使用$q.when(value)。它会创建承诺,立即解析为value

尝试:

var response = {data: asset};
var spy = spyOn(assetFactory, 'getAsset').and.returnValue($q.when(response));

当然,您需要在测试中注入$q

还值得阅读How to unit-test promise-based code in Angular

答案 1 :(得分:0)

我也有这个问题,但上面的解决方案无能为力。我的问题有点不同。我之前使用的是模拟。

jasmine.createSpy('mockFunc()').and.callFake()

然后我用回调功能将其改为如下所示。

jasmine.createSpy('mockFunc()').and.callFake(function() { })

我知道这听起来很疯狂,但这解决了我的问题。如果你犯了同样的错误,你也可以尝试一下。

相关问题