Angular Karma Test得到“TypeError:'undefined'不是函数”

时间:2015-03-06 09:05:25

标签: javascript angularjs karma-runner karma-jasmine

  

控制器

dashboardApp.controller('ExecutiveController', ['$scope',   'ExecutiveService',
function ($scope, executiveService) {

    executiveService.getDashboardData().then(
    function success(response) {
        $scope.productsInfo = response.data;
    }, function error(reason) {
        console.log(reason);
    });

} ]);
  

Controller的测试用例       '使用严格的';

describe('ExecutiveController', function () {
var scope, ctrl, mockService, $timeout;

beforeEach(module('do.dashboard'));

beforeEach(inject(function ($rootScope, $controller, $q, _$timeout_) {

    mockService = jasmine.createSpyObj('ExecutiveService', ['getDashboardData']);
    scope = $rootScope.$new();
    $timeout = _$timeout_;
    mockService.getDashboardData.andReturn($q.when(

        [{Products:{Name:'Resource ',measure:'59',Work:'60'}}]

    ));

    ctrl = $controller('ExecutiveController', {
        $scope: scope,
        ExecutiveService: mockService,

    });
}));


it('controller should not be null', function () {
    expect(ctrl).not.toBe(null);
});


it('should call the service method and set the scope data', function () {
    expect(mockService.getDashboardData).toHaveBeenCalled();
    $timeout.flush();
    expect(scope.productsInfo.Products.Name).toEqual('Resource');
    expect(scope.productsInfo.products.measure).toEqual(59);


});

});
  

这里当我在karma运行器中运行它时会显示错误,因为Angular Karma Test会出现“TypeError:' undefined'不是一个函数“并且它给出了it方法中存在的消息

1 个答案:

答案 0 :(得分:0)

您的Jasmine 2.0语法错误 - 应该是

mockService.getDashboardData.and.returnValue

mockService.getDashboardData.andReturn

http://jsfiddle.net/xqa41at0/