【发布时间】:2014-01-16 16:02:01
【问题描述】:
我有以下情况:
controller.js
controller('PublishersCtrl',['$scope','APIService','$timeout', function($scope,APIService,$timeout) {
APIService.get_publisher_list().then(function(data){
});
}));
controllerSpec.js
'use strict';
describe('controllers', function(){
var scope, ctrl, timeout;
beforeEach(module('controllers'));
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new(); // this is what you missed out
timeout = {};
controller = $controller('PublishersCtrl', {
$scope: scope,
APIService: APIService,
$timeout: timeout
});
}));
it('should have scope variable equals number', function() {
expect(scope.number).toBe(3);
});
});
错误:
TypeError: Object #<Object> has no method 'get_publisher_list'
我也尝试过类似的方法,但没有成功:
describe('controllers', function(){
var scope, ctrl, timeout,APIService;
beforeEach(module('controllers'));
beforeEach(module(function($provide) {
var service = {
get_publisher_list: function () {
return true;
}
};
$provide.value('APIService', service);
}));
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
timeout = {};
controller = $controller('PublishersCtrl', {
$scope: scope,
APIService: APIService,
$timeout: timeout
}
);
}));
it('should have scope variable equals number', function() {
spyOn(service, 'APIService');
scope.get_publisher_list();
expect(scope.number).toBe(3);
});
});
我该如何解决这个问题?有什么建议吗?
【问题讨论】:
-
你的
controller.js语法错误 -
@dcodesmith 是的,我缩短了它,因为真正重要的是函数调用,忽略所有其余 APIService.get_publisher_list()
-
除非您将严格的声明放入函数中,否则我拒绝提供帮助:pbs.twimg.com/media/BcMF93NCQAAXKhW.jpg
标签: unit-testing angularjs karma-runner karma-jasmine