尝试用 jasmine 测试 angular 控制器 - 错误
Trying to test an angular controller with jasmine - errors
我是 angular/jasmine/karma 的新手,在为我的控制器编写测试时遇到了问题。测试本身 运行 成功,但在测试 运行 中出现以下错误:
Error: userFactory() method does not exist
userFactory() 是在我的控制器中对 returns 承诺的服务进行的方法调用。我不确定如何确保在测试中正确定义了它。
这是我的代码:
app.js
(function () {
angular.module('mdotTamcCouncil', ['mdotTamcCouncil.core', 'blurb']);
angular.module('mdotTamcCouncil.core', []);
})();
blurb-service.js
(function () {
angular.module('mdotTamcCouncil.core').factory('blurbsFactory', function ($http) {
var promise = null;
return function () {
if (promise) {
// If we've already asked for this data once,
// return the promise that already exists.
return promise;
} else {
promise = $http.get(jsGlobals.blurbsDataURL);
return promise;
}
};
});
})();
用户-service.js
(function () {
angular.module('mdotTamcCouncil.core').factory('userFactory', function ($http) {
var promise = null;
return function () {
if (promise) {
// If we've already asked for this data once,
// return the promise that already exists.
return promise;
} else {
promise = $http.get(jsGlobals.userDataURL);
return promise;
}
};
});
})();
blurb-controller.js
(function () {
angular.module('blurb')
.controller('BlurbController', ['$scope', 'blurbsFactory', 'userFactory', function ($scope, blurbsFactory, userFactory) {
$scope.content = "";
$scope.blurbs = {};
$scope.currentUser = {};
this.editMode = false;
userFactory().success(function (data) {
$scope.currentUser = data;
});
blurbsFactory().success(function (data) {
$scope.blurbs = data;
$scope.content = $scope.blurbs[$scope.textKey];
});
this.enterEditMode = function () {
this.editMode = true;
};
this.saveEdits = function () {
this.editMode = false;
$scope.blurbs[$scope.textKey] = $scope.content;
};
}]);
})();
blurb-module.js
(function () {
'use strict';
angular.module('blurb', ['ngSanitize', 'mdotTamcCouncil.core']);
})();
和我的测试规范:
describe('BlurbController', function () {
var scope, controllerService;
beforeEach(module('mdotTamcCouncil'));
beforeEach(module('mdotTamcCouncil.core'));
beforeEach(module('blurb'));
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controllerService = $controller;
}));
it("should get 'user' from 'data/user.json'", inject(function ($httpBackend) {
$httpBackend.expectGET("data/user.json").respond({"userName": "myera","email": "something@something.com","isAdmin": true});
$httpBackend.expectGET("data/blurbs.json").respond('{"mainPageIntro": "<h2>Welcome</h2>"}');
ctrl = controllerService('BlurbController', { $scope: scope });
$httpBackend.flush();
expect(scope.currentUser).toEqual({"userName": "myera","email": "something@something.com","isAdmin": true});
expect(scope.blurbs).toEqual({ "mainPageIntro": "<h2>Welcome</h2>" });
}));
});
我通过阅读博客和 Whosebug 答案拼凑了这个。我什至不确定我是否做对了。
测试本身实际上通过了,但我在控制台中收到有关缺少 "userFactory()" 方法的错误。我想如果 "blurbFactory()" 方法已经那么远了,我也会收到消息。我不相信我可以在不首先解决这些错误的情况下测试控制器的实际功能。
我做错了什么?非常感谢您提供的任何帮助。
我认为您遇到的问题是因为您没有将 'mdotTamcCouncil.core' 模块作为依赖项包含在 blurb 模块中。定义它时,您应该能够使用
传入依赖项列表
angular.module('blurb', ['mdotTamcCouncil.core']);
您只收到一个控制台错误,因为当 userFactory() 失败时执行停止。我不确定为什么测试显示为通过,它应该会发现异常并失败 - 可能与您选择的测试运行程序不一致。
我是 angular/jasmine/karma 的新手,在为我的控制器编写测试时遇到了问题。测试本身 运行 成功,但在测试 运行 中出现以下错误:
Error: userFactory() method does not exist
userFactory() 是在我的控制器中对 returns 承诺的服务进行的方法调用。我不确定如何确保在测试中正确定义了它。
这是我的代码:
app.js
(function () {
angular.module('mdotTamcCouncil', ['mdotTamcCouncil.core', 'blurb']);
angular.module('mdotTamcCouncil.core', []);
})();
blurb-service.js
(function () {
angular.module('mdotTamcCouncil.core').factory('blurbsFactory', function ($http) {
var promise = null;
return function () {
if (promise) {
// If we've already asked for this data once,
// return the promise that already exists.
return promise;
} else {
promise = $http.get(jsGlobals.blurbsDataURL);
return promise;
}
};
});
})();
用户-service.js
(function () {
angular.module('mdotTamcCouncil.core').factory('userFactory', function ($http) {
var promise = null;
return function () {
if (promise) {
// If we've already asked for this data once,
// return the promise that already exists.
return promise;
} else {
promise = $http.get(jsGlobals.userDataURL);
return promise;
}
};
});
})();
blurb-controller.js
(function () {
angular.module('blurb')
.controller('BlurbController', ['$scope', 'blurbsFactory', 'userFactory', function ($scope, blurbsFactory, userFactory) {
$scope.content = "";
$scope.blurbs = {};
$scope.currentUser = {};
this.editMode = false;
userFactory().success(function (data) {
$scope.currentUser = data;
});
blurbsFactory().success(function (data) {
$scope.blurbs = data;
$scope.content = $scope.blurbs[$scope.textKey];
});
this.enterEditMode = function () {
this.editMode = true;
};
this.saveEdits = function () {
this.editMode = false;
$scope.blurbs[$scope.textKey] = $scope.content;
};
}]);
})();
blurb-module.js
(function () {
'use strict';
angular.module('blurb', ['ngSanitize', 'mdotTamcCouncil.core']);
})();
和我的测试规范:
describe('BlurbController', function () {
var scope, controllerService;
beforeEach(module('mdotTamcCouncil'));
beforeEach(module('mdotTamcCouncil.core'));
beforeEach(module('blurb'));
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controllerService = $controller;
}));
it("should get 'user' from 'data/user.json'", inject(function ($httpBackend) {
$httpBackend.expectGET("data/user.json").respond({"userName": "myera","email": "something@something.com","isAdmin": true});
$httpBackend.expectGET("data/blurbs.json").respond('{"mainPageIntro": "<h2>Welcome</h2>"}');
ctrl = controllerService('BlurbController', { $scope: scope });
$httpBackend.flush();
expect(scope.currentUser).toEqual({"userName": "myera","email": "something@something.com","isAdmin": true});
expect(scope.blurbs).toEqual({ "mainPageIntro": "<h2>Welcome</h2>" });
}));
});
我通过阅读博客和 Whosebug 答案拼凑了这个。我什至不确定我是否做对了。
测试本身实际上通过了,但我在控制台中收到有关缺少 "userFactory()" 方法的错误。我想如果 "blurbFactory()" 方法已经那么远了,我也会收到消息。我不相信我可以在不首先解决这些错误的情况下测试控制器的实际功能。
我做错了什么?非常感谢您提供的任何帮助。
我认为您遇到的问题是因为您没有将 'mdotTamcCouncil.core' 模块作为依赖项包含在 blurb 模块中。定义它时,您应该能够使用
传入依赖项列表angular.module('blurb', ['mdotTamcCouncil.core']);
您只收到一个控制台错误,因为当 userFactory() 失败时执行停止。我不确定为什么测试显示为通过,它应该会发现异常并失败 - 可能与您选择的测试运行程序不一致。