嗨,我有这个简单的测试:
define(["angular", "angularMocks", "app", "normalizer"], function(angular, mocks, app) {
describe("service: normalizer", function () {
var normalizerService;
beforeEach(module("ADB"));
beforeEach(inject(function(_normalizer_) {
normalizerService = _normalizer_;
}));
var params = {};
var metadata = {};
var data = {};
var response = normalizerService.topLanguagesHybrid(metadata, data, params);
var type = typeof response;
expect(type).toEqual("object");
});
});
问题是从未设置normalizer服务,在cmd中我看到以下错误:
TypeError: 'undefined' is not an object (evaluating 'normalizerService.topLanguagesHybrid')
注意:我在这个项目中使用requirejs,我可以看到normalizer服务文件正在加载到浏览器中(包含所有依赖项)。似乎它没有注入。 我做错了什么?
答案 0 :(得分:1)
定义服务时,您忘记定义规范化程序,因此正确的语法是
define(["angular", "angularMocks", "app", "normalizer"], function(angular, mocks, app, normalizerService) {
describe("service: normalizer", function () {
var normalizerService;
beforeEach(module("ADB"));
var params = {};
var metadata = {};
var data = {};
var response = normalizerService.topLanguagesHybrid(metadata, data, params);
var type = typeof response;
expect(type).toEqual("object");
});
});
答案 1 :(得分:1)
对于测试场景,你忘了为它定义它,之后每个运行之前运行它"它"功能
define(["angular", "angularMocks", "app", "normalizer"], function(angular, mocks, app) {
describe("service: normalizer", function () {
var normalizerService;
beforeEach(module("ADB"));
beforeEach(inject(function(_normalizer_) {
normalizerService = _normalizer_;
}));
it('should define topLanguageHybrid method', function() {
var params = {};
var metadata = {};
var data = {};
var response = normalizerService.topLanguagesHybrid(metadata, data, params);
var type = typeof response;
expect(type).toEqual("object");
});
});
});
有关jasmine的更多信息:jasmine docs