使用Jasmine和Karma的AngularJS控制器的单元测试“无法读取属性'然后'未定义

时间:2016-10-14 15:00:12

标签: javascript angularjs jasmine karma-jasmine

我正在尝试对angularjs控制器进行单元测试,并在运行Karma时收到此错误消息:

  

无法读取未定义的属性'然后'

我做错了什么? 对不起,这是我第一次测试一些东西。

控制器:

<div id="card"></div>

测试:

angular
    .module('my')
    .controller('MyCtrl', MyController);

MyController.$inject = ['$scope', 'myFactory'];

function MyController($scope,myFactory) {

    $scope.thingy = {};

//[..]
    function getThingys() {
        myFactory.getThingys(function () {}).then(function (data) {
            //SUCCESS
            $scope.thingy = data;
        });
    } 
}

1 个答案:

答案 0 :(得分:0)

模拟服务:

 var $httpBackend;
 var myFactory; 

 beforeEach(angular.mock.inject(function ($injector) {    
        // Set up the mock http service responses
        $httpBackend = $injector.get('$httpBackend');

        // backend definition common for all tests
        myFactory= $injector.get('myFactory');

        $httpBackend.when('GET', 'url for call').respond({mock data});
}

 afterEach(function () {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    });