Javascript Angular JS UT:具有返回$resource方法的服务的控制器

Javascript Angular JS UT:具有返回$resource方法的服务的控制器,javascript,angularjs,unit-testing,karma-mocha,bardjs,Javascript,Angularjs,Unit Testing,Karma Mocha,Bardjs,我正在为我的角度控制器编写一个单元测试;正在从服务接收$resource对象 然而,单元测试失败了,因为“Action.query(success)”不是一个函数 期待您的评论 PhantomJS 1.9.8 (Windows 8 0.0.0) ActionController Action controller getList() call should return an instance of array FAILED TypeError: '[object Object]' is not

我正在为我的角度控制器编写一个单元测试;正在从服务接收$resource对象

然而,单元测试失败了,因为“Action.query(success)”不是一个函数

期待您的评论

PhantomJS 1.9.8 (Windows 8 0.0.0) ActionController Action controller getList() call should return an instance of array FAILED
TypeError: '[object Object]' is not a function (evaluating 'Action.query(success)')
action.controller.js

(function() {
'use strict';

angular
    .module('app.action')
    .controller('ActionController', ActionController);

ActionController.$inject = ['$sce', 'ActionService'];

/* @ngInject */
function ActionController($sce, $state, $stateParams, logger, exception,
                           moduleHelper, httpHelper, actionService) {
    var vm = this;       
    var Action = null;

    vm.title = 'action';        
    vm.data = []; /* action list model */

    vm.getList = getList;

    activate();

    //////////////// 
    function activate() {
        Action = actionService.action();
    }

    /**
     * Provides list of actions.
     * Used from list.html
     */
    function getList() {
        var data = Action.query(success);

        function success() {
            vm.data = data._embedded.actions;
            return vm.data;
        }
    }
}
})();
action.service.js

(function () {
'use strict';
angular
    .module('app.action')
    .service('ActionService', ActionService);

ActionService.$inject = ['$resource'];

/* @ngInject */
function  ActionService($resource) {
    var module = 'action';
    var exports = {
        action: action
    };

    return exports;

    ////////////////

    /**
     * Provides $resource to action controller
     * @returns {Resources} Resource actions
     */
    function action() {
        return $resource('app/actions/:id', {id: '@id'}, {
            query:{
                method: 'Get',
                isArray: false
            },
            update: {
                method: 'PUT'
            }
        });
    }
}
})();
action.controller.spec.js

/* jshint -W117, -W030 */
describe('ActionController', function() {
var controller;
var mockActions = mockData.getMockActions();
var mConfig = mockActions.getConfig();
var mockService = function() {
        var list = [{
            'id' : 1,'name' : 'CREATE'
        },{
            'id' : 2,'name' : 'VIEW'
        }];

        return {
            query: function() {
                return list;
            },
            get: function() {
                return list[0];
            },
            save: function(action) {
                var length = list.length;
                list.push(action);
                return ((length + 1) === list.length);
            },
            update: function(action) {
                return true;
            }
        };
    }

beforeEach(function() {
    bard.appModule('app.action');
    bard.inject('$controller', '$q', '$rootScope','ActionService');
});

beforeEach(function () {
    bard.mockService(ActionService, {
        action: function() {
            return {
                query: $q.when(mockService.query()),
                get: $q.when(mockService.get()),
                save: function(action) {
                    return $q.when(mockService.save(action));
                },
                update: function(action) {
                    return $q.when(mockService.update(action));
                },
            };
        },
        _default:    $q.when([])
    });
    controller = $controller('ActionController');
    $rootScope.$apply();
});

bard.verifyNoOutstandingHttpRequests();

describe('Action controller', function() {
    it('should be created successfully', function () {
        expect(controller).to.be.defined;
    });

    describe('getList() call', function () {                
            it('should have getList defined', function () {
                expect(controller.getList).to.be.defined;
            });
            it('should return an instance of array', function () {
                /* getting an error here*/
                expect(controller.getList()).to.be.insanceOf(Array);
            });
            it('should return an array of length 2', function () {
                expect(controller.getList()).to.have.length(2);
            });
        });
    });
});
});

$inject
数组中值的顺序必须与
ActionController
中参数的顺序相匹配

ActionController.$inject = ['$sce', 'ActionService'];

/* @ngInject */
// 'actionService' must be the second parameter in the 'ActionController' function.
function ActionController($sce, actionService, $state, $stateParams, logger, exception,
                       moduleHelper, httpHelper) {
    var vm = this;       
    var Action = null;

    // the rest of the code.
您可以在此处找到更多信息: