我试图对使用restangular的服务进行单元测试。我已经尝试了几种方法在单元中包含restangular,第一种是在beforeEach中这样做:
beforeEach(angular.mock.module("restangular"));
上面的内容可以在restangular单元测试中看到。
我也试过了。
beforeEach(angular.module("restangular"));
然后我最后尝试了以下内容:http://plnkr.co/edit/hNmxWR?p=info
it('should inject restangular', inject(function(Restangular) {
expect(Restangular).toBeDefined();
//... do stuff with
}));
每次运行单元测试时都会出现以下错误:
Failed to instantiate module restangular due to:
Error: [$injector:nomod] Module 'restangular' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.4.7/$injector/nomod?p0=restangular
有什么想法吗?这就是我的服务示例:
angular
.module('schoolsService' ['restangular'])
.factory('schoolsService', function(Restangular) {
var schools = Restangular.all('/report/school');
return {
getSchools: function(schoolType) {
return schools.get(schoolType);
}
};
});
答案 0 :(得分:1)
我就是这样做的:
describe("schoolsService test", function () {
beforeEach(module("schoolsService"));
beforeEach(module("restangular"));
var schoolsService, Restangular;
beforeEach(inject(function (_schoolsService_, _Restangular_){
schoolsService = _schoolsService_;
Restangular = _Restangular_;
});
it("performs a test", function(){
// expect something
});
});
另外,请记住在配置文件中包含这些模块!