Angularjs单元测试解决了承诺

时间:2015-05-14 15:33:27

标签: javascript angularjs unit-testing jasmine spy

我可能做错了,但我无法弄清楚如何修复它。

我想测试一个使用资源的控制器(ngResource),我想使用Spy作为资源的测试双重,因此它实际上并没有进行http调用。在下面的代码中,我只想测试控制器中的搜索功能。

控制器:

controllers = angular.module('app.controllers');
controllers.controller('landingCtrl', ['$scope', '$q', 'categoryResource', function ($scope, $q, categoryResource) {

    $scope.search = function (text) {
        console.log('searching for: ' + text);
        var deferred = $q.defer();
        categoryResource.query({ searchTerm: text }, function (result) {
            if (result.length == 0) {
                deferred.resolve(['No results found']);
            } else {
                deferred.resolve(result);
            }
        });
        return deferred.promise;
    }
}]);

服务:

var services = angular.module('app.services');    
services.factory('categoryResource', ['$resource', function ($resource) {
    var resource = $resource('/api/category');    
    return resource;
}]);

landingCtrl的规范:

describe('Controller: landingCtrl ', function () {

    var $q,
        $rootScope,
        $scope;

    beforeEach(module('ngResource'));
    beforeEach(module('app.services'));
    beforeEach(module('app.controllers'));

    beforeEach(inject(function (_$rootScope_, _$q_) {
        $q = _$q_;
        $rootScope = _$rootScope_;
    }));

    // mock any depencencies, like scope. $resource or $http
    beforeEach(inject(function ($controller, $injector, categoryResource) {
        $scope = $rootScope.$new();

        spyOn(categoryResource, 'query').andCallFake(function (searchText) {
            console.log('query fake being called');
            var deferred = $q.defer();
            deferred.resolve(['Test', 'Testing', 'Protester']);
            return deferred.promise;
        });

        landingCtrl = $controller('landingCtrl', {
            '$scope': $scope,
            '$q': $q,
            'categoryResource': categoryResource
        });
    }));

    afterEach(inject(function ($rootScope) {
        $rootScope.$apply();
    }));

    it('should return words with "test" in them"', function () {
        $scope.search('test').then(function (results) {
            console.log(results);
            expect(results).toContain('Test');
        });
        $scope.$apply();
    });
});

测试执行时没有错误,但是它没有解析承诺,所以我的代码在"然后"函数永远不会被调用。我做错了什么?

我已经创建了一个带有上述内容的测试者,并且测试应该失败:

http://plnkr.co/edit/adE6fTajgbDoM33rtbZS?p=preview

1 个答案:

答案 0 :(得分:3)

您的规范正在模仿categoryResource.query(),因此它会返回一个承诺,但您的控制器并不期望这样。它调用query()并传递一个回调,并在该回调中完成它的事情。换句话说,您的规范不会测试控制器的功能。

这是您的固定规格:

spyOn(categoryResource, 'query').andCallFake(function (searchText, callback) {
    console.log('query fake being called');
    callback(['Test', 'Testing', 'Protester']);
});

...

it('should return words with "test" in them"', function () {
    var results;

    $scope.search('test').then(function (_results_) {
        console.log(results);
        results = _results_;
    });
    $scope.$apply();

    expect(results).toContain('Test');
});

Working Plunker

请注意,我已将期望移至then()回调之外,因此如果未解决承诺,您的测试会中断。