我正在尝试为一个函数编写 jasmine 测试用例,该函数又调用另一个函数并返回响应。似乎调用了函数,但我没有得到函数内部设置的值。
控制器功能:
angular.module('serviceApp')
.controller('startWorkscopeCtrl', function ($rootScope, $log, $scope, CommentService) {
$scope.getTextById = function (id) {
CommentService.get({ id: id }, function (response) {
$scope.text = response;
$scope.textCount = $scope.text.length;
});
};
});
该服务在另一个 js 文件中被引用,它在其中对后端服务进行其余服务调用。
angular.module('restServiceModule', ['ngResource'])
.factory('CommentService', function($resource){
return $resource(
{
get: {
method: 'GET',
isArray: true,
url: window.urlPrefix + '/commentservice/:id',
params: {
id: '@id'
}
}
});
})
测试用例:
var mockTextComment;
var fakeText = ['testPass'];
beforeEach(function(){
mockTextComment = {
get: function(){
return fakeText;
}
};
});
it('should get comments by asset id', function () {
spyOn(mockTextComment, 'get');
inject(function ($controller) {
ctrl = $controller('startWorkscopeCtrl', {
$scope: scope,
CommentService: mockTextComment
});
scope.getTextById(40107);
expect(scope.textCount).toEqual(1);
});
});
有了这个,我收到以下错误:
**Expected undefined to equal 1.
Error: Expected undefined to equal 1.**
不知道我在哪里做错了。对此的任何指示都会有所帮助。