angularJS 仅使用独立的 jasmine 服务器进行工厂测试
angularJS factory testing using a stand-alone jasmine server only
我正在学习如何仅使用独立的 jasmine 服务器编写 AngularJS 测试。
我成功地能够测试控制器但是,我在测试我编写的工厂时遇到了问题。我收到类似
的错误
Error: [$injector:modulerr]
(工厂代码)myservice.js:
mod.factory('WeatherService', function ($http) {
return {
testingdata: function () {
return {name: "test", class: "ix"};
}
};});
app.js
var mod = angular.module("myapp", []);
我的测试:
describe("ws", function () {
describe('when I call WeatherService', function () {
it('returns 1', function () {
var t = {name: "test", class: "ix"};
var $injector = angular.injector(['myapp']);
var WeatherService= $injector.get('WeatherService');
expect(WeatherService.testingdata).toEqual(t);
});
});
});
你的测试太复杂了。
你不要在测试中使用 angular.injector - 你可以使用 ngMock
中的模块函数
类似地,您可以使用 ngMock 模块中的函数注入代替 $injector.get。
describe("ws", function() {
beforeEach(module('myapp'));
describe('when I call WeatherService', function() {
it('returns 1', inject(function(WeatherService) {
var t = {
name: "test",
class: "ix"
};
expect(WeatherService.testingdata()).toEqual(t);
}));
});
});
我正在学习如何仅使用独立的 jasmine 服务器编写 AngularJS 测试。 我成功地能够测试控制器但是,我在测试我编写的工厂时遇到了问题。我收到类似
的错误Error: [$injector:modulerr]
(工厂代码)myservice.js:
mod.factory('WeatherService', function ($http) {
return {
testingdata: function () {
return {name: "test", class: "ix"};
}
};});
app.js
var mod = angular.module("myapp", []);
我的测试:
describe("ws", function () {
describe('when I call WeatherService', function () {
it('returns 1', function () {
var t = {name: "test", class: "ix"};
var $injector = angular.injector(['myapp']);
var WeatherService= $injector.get('WeatherService');
expect(WeatherService.testingdata).toEqual(t);
});
});
});
你的测试太复杂了。
你不要在测试中使用 angular.injector - 你可以使用 ngMock
中的模块函数类似地,您可以使用 ngMock 模块中的函数注入代替 $injector.get。
describe("ws", function() {
beforeEach(module('myapp'));
describe('when I call WeatherService', function() {
it('returns 1', inject(function(WeatherService) {
var t = {
name: "test",
class: "ix"
};
expect(WeatherService.testingdata()).toEqual(t);
}));
});
});