[英]angularJS factory testing using a stand-alone jasmine server only
I am learning how to write AngularJS tests using a stand-alone jasmine server only. 我正在学习如何仅使用独立的茉莉花服务器编写AngularJS测试。 I was successfully able to test a controller however, I'm having trouble testing a factory that I wrote.
我可以成功测试控制器,但是无法测试自己编写的工厂。 I get an error like,
我收到类似的错误,
Error: [$injector:modulerr]
(Factory code) myservice.js: (工厂代码)myservice.js:
mod.factory('WeatherService', function ($http) {
return {
testingdata: function () {
return {name: "test", class: "ix"};
}
};});
app.js app.js
var mod = angular.module("myapp", []);
my test: 我的测试:
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);
});
});
});
Your test is unnecessarily complicated. 您的测试不必要地复杂。
You do not use angular.injector in test - you can use module function from ngMock 您没有在测试中使用angular.injector-您可以使用ngMock中的模块功能
Similarly instead of $injector.get you can use function inject from ngMock module. 类似地,您可以使用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);
}));
});
});
here is working example http://plnkr.co/edit/doBJeWRyVyD75LfRP4G4?p=preview 这是工作示例http://plnkr.co/edit/doBJeWRyVyD75LfRP4G4?p=preview
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.