在 Jasmine 中调用服务方法
Invoking service method in Jasmine
使用 Angular 和 Jasmine 我想 运行 带有一些模型数据的服务方法。下面是我的测试代码,它使用一些正在工作的 RoomsController 尝试 运行 RoomsParamsSvc 上的 test() 方法:
describe('Rooms Controller', function() {
var RoomsController,
scope,
location,
httpBackend,
RoomsParamsSvc;
beforeEach(module('rooms', function ($provide, $injector) {
RoomsParamsSvc = function () { //(1a)
return $injector.get('RoomsParamsSvc'); //(1b)
}; //(1c)
$provide.value('RoomsParamsSvc', RoomsParamsSvc); //(1d)
}));
beforeEach(inject(function($controller, $rootScope, $location, $httpBackend, _RoomsParamsSvc_) {
// Set a new global scope
scope = $rootScope.$new();
location = $location;
httpBackend = $httpBackend;
RoomsParamsSvc = _RoomsParamsSvc_;
RoomsController = $controller('RoomsController', {
$scope: scope,
$location: location,
RoomsParamsSvc: RoomsParamsSvc
});
}));
it('should have test as a function', function () {
var t = RoomsParamsSvc.test();
});
});
据我对 with 注入器的了解,我应该能够使用注入的服务。没有 (1a-1d) 我得到一个错误:
Error: [$injector:unpr] Unknown provider: RoomsParamsSvcProvider <-
RoomsParamsSvc
不过现在也行不通了。我得到一个错误,意思是 test() 不是一个函数:
jasmine typeerror 'undefined' is not a function (evaluating 'RoomsParamsSvc.test()')
我的服务是这样的:
var roomsApp = angular.module('rooms', []);
roomsApp.factory('RoomsParamsSvc', function () {
var factory = {};
factory.test = function ()
{
return '';
}
return factory;
});
你有什么建议吗?
第 1a-1d 行不是必需的,因为 'RoomsParamsSvc' 已加载到您的 'room' 模块中。但是您引用了未定义的 RoomsController。
beforeEach(module('rooms'));
beforeEach(inject(function($controller, $rootScope, $location, $httpBackend, _RoomsParamsSvc_) {
// Set a new global scope
scope = $rootScope.$new();
location = $location;
httpBackend = $httpBackend;
RoomsParamsSvc = _RoomsParamsSvc_;
RoomsController = $controller(function() {}, {
$scope: scope,
$location: location,
RoomsParamsSvc: RoomsParamsSvc
});
console.log(RoomsParamsSvc);
}));
使用 Angular 和 Jasmine 我想 运行 带有一些模型数据的服务方法。下面是我的测试代码,它使用一些正在工作的 RoomsController 尝试 运行 RoomsParamsSvc 上的 test() 方法:
describe('Rooms Controller', function() {
var RoomsController,
scope,
location,
httpBackend,
RoomsParamsSvc;
beforeEach(module('rooms', function ($provide, $injector) {
RoomsParamsSvc = function () { //(1a)
return $injector.get('RoomsParamsSvc'); //(1b)
}; //(1c)
$provide.value('RoomsParamsSvc', RoomsParamsSvc); //(1d)
}));
beforeEach(inject(function($controller, $rootScope, $location, $httpBackend, _RoomsParamsSvc_) {
// Set a new global scope
scope = $rootScope.$new();
location = $location;
httpBackend = $httpBackend;
RoomsParamsSvc = _RoomsParamsSvc_;
RoomsController = $controller('RoomsController', {
$scope: scope,
$location: location,
RoomsParamsSvc: RoomsParamsSvc
});
}));
it('should have test as a function', function () {
var t = RoomsParamsSvc.test();
});
});
据我对 with 注入器的了解,我应该能够使用注入的服务。没有 (1a-1d) 我得到一个错误:
Error: [$injector:unpr] Unknown provider: RoomsParamsSvcProvider <- RoomsParamsSvc
不过现在也行不通了。我得到一个错误,意思是 test() 不是一个函数:
jasmine typeerror 'undefined' is not a function (evaluating 'RoomsParamsSvc.test()')
我的服务是这样的:
var roomsApp = angular.module('rooms', []);
roomsApp.factory('RoomsParamsSvc', function () {
var factory = {};
factory.test = function ()
{
return '';
}
return factory;
});
你有什么建议吗?
第 1a-1d 行不是必需的,因为 'RoomsParamsSvc' 已加载到您的 'room' 模块中。但是您引用了未定义的 RoomsController。
beforeEach(module('rooms'));
beforeEach(inject(function($controller, $rootScope, $location, $httpBackend, _RoomsParamsSvc_) {
// Set a new global scope
scope = $rootScope.$new();
location = $location;
httpBackend = $httpBackend;
RoomsParamsSvc = _RoomsParamsSvc_;
RoomsController = $controller(function() {}, {
$scope: scope,
$location: location,
RoomsParamsSvc: RoomsParamsSvc
});
console.log(RoomsParamsSvc);
}));