Javascript 使模块在jasmine通过Resharper测试的控制器中可用
我可以使用Resharper 9.2通过PhantomJs成功地测试使用jasmine的控制器。作为一名测试者 我按照上的说明安装了Resharper 这起作用了:如果我没有指定控制器所依赖的模块,我可以为控制器运行测试: 控制器:Javascript 使模块在jasmine通过Resharper测试的控制器中可用,javascript,angularjs,jasmine,resharper,Javascript,Angularjs,Jasmine,Resharper,我可以使用Resharper 9.2通过PhantomJs成功地测试使用jasmine的控制器。作为一名测试者 我按照上的说明安装了Resharper 这起作用了:如果我没有指定控制器所依赖的模块,我可以为控制器运行测试: 控制器: var moduleName; (function (moduleName) { 'use strict'; var testableController = (function () { function testableC
var moduleName;
(function (moduleName) {
'use strict';
var testableController = (function () {
function testableController($scope) {
var _this = this;
this.$scope = $scope;
$scope.title = "Welcome";
}
testableController.className = 'testableController';
return testableController;
}());
moduleName.testableController = testableController;
})(moduleName || (moduleName = {}));
等级库文件如下所示
///<reference path="~/Scripts/jasmine/jasmine.js"/>
///<reference path="~/Scripts/jasmine/angular.js"/>
///<reference path="~/Scripts/jasmine/angular-mocks.js"/>
///<reference path="~/Scripts/angular-ui/ui-bootstrap.min.js" />
///<reference path="~/Scripts/jasmine/controllers.js"/>
///<reference path="~/Scripts/App/Controllers/testableController.js" />
///<reference path="~/Scripts/App/AppJasmine.js" />
describe("Controllers", function() {
beforeEach(module("moduleName"));
describe("Jasmine testableController", function () {
var scope,
controller;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('testableController', { $scope: scope });
}));
it('should set the page title as "Welcome"', function () {
expect(scope.title).toBe('Welcome');
});
});
});
**编辑1**
我试过了
但也有同样的错误
编辑2
我用
版本:1.3.3-2016-05-22
AngularJS v1.2.24
编辑3
我不想测试$uibModal,但要模拟它我知道两种模拟服务的策略,有多种语法变体,就像Angular中的一切一样。。。您只需将对象文字添加到控制器声明中,也可以创建自己的服务,并使用$provider将其添加到模块中: 如果服务只是某个数据层或API的包装器,则可以使用文字对象模拟其功能,并在控制器构造函数中插入(如果正确的话),遵循示例语法,可以这样做:
var currentAuth;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
currentAuth = {uid: 1, name: juan, getFriends: function() { ... }};
controller = $controller('TestableCtrl', {'$scope': $scope, 'currentAuth': currentAuth });
}));
在这种情况下,“currentAuth”是一种服务,它提供应用程序中当前登录的用户
仅当不需要在对象中定义的函数中注入任何服务时,此选项才有效。这相当于创建一个价值服务并将其注入模块中。如果模拟服务中的方法本身需要任何服务,则必须创建一个工厂或服务,将此服务添加到模块中,然后像任何其他自定义服务一样将其注入。请参见我用来模拟angularfire身份验证服务的示例:
var $controller, $rootScope, $scope, $location, Auth;
beforeEach(function(){
module('planner.login');
module(function($provide){
$provide.factory('Auth', function($q){
return {
$signInWithEmailAndPassword: function(email, pass) {
return $q.reject({error: 'ERROR'});
}
};
});
return null;
});
});
beforeEach(function(){
inject(function($controller, $rootScope, _Auth_) {
$scope = $rootScope.$new();
Auth = _Auth_;
$controller("TestableCtrl", {
$scope: $scope,
Auth: Auth,
$stateParams: {}
});
});
});
在这个示例中,我创建了一个新的工厂,它使用$q服务返回角度承诺(不需要在Chrome上进行测试,但PhantomJS没有承诺规范)。请注意,在执行此操作之前,您需要两个接口,一个用于将提供程序添加到模块,另一个用于将提供程序注入控制器
使用哪一个取决于您想要测试什么,以及您需要模拟原始服务行为的深度。对于uibmodal,您可能需要在某个点调用“.open”并监视已调用的对象,但您只需要创建一个具有该属性的对象并监视该对象的对象属性。因此,第一种方法应该足够了
因此,您的代码应该类似于:
describe("Controllers", function() {
beforeEach(module("moduleName"));
describe("Jasmine testableController", function () {
var scope,
controller,
uibModal;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
uibModal = { open: function() { return 'Whatever'; } }
controller = $controller('testableController', { $scope: scope, $uibModal: uibModal });
}));
it('should set the page title as "Welcome"', function () {
expect(scope.title).toBe('Welcome');
// you will probably do something like this at some point to verify
// that the modal gets opened on click or following any other action:
// var spy = spyOn(uibModal, 'open');
// expect(spy).toHaveBeenCalled();
});
});
});
我希望这能有所帮助,如果有什么不清楚的地方,请告诉我,我也在学习如何测试AngularJS应用程序,我很想知道这对AngularUI是否有帮助。第一个问题是您没有将控制器注册到模块。因此,它不知道模块的依赖关系
angular.module('modulename').controller('nameofthecontroller', controller);
然后您可以模拟$uibModal,但不需要它;它应该在没有这种嘲弄的情况下工作 您正在使用哪个版本的ui.bootstrap和哪个版本的angular?请参阅更新的问题ui.bootstrap 1.3.3,angular v1.2.24
var $controller, $rootScope, $scope, $location, Auth;
beforeEach(function(){
module('planner.login');
module(function($provide){
$provide.factory('Auth', function($q){
return {
$signInWithEmailAndPassword: function(email, pass) {
return $q.reject({error: 'ERROR'});
}
};
});
return null;
});
});
beforeEach(function(){
inject(function($controller, $rootScope, _Auth_) {
$scope = $rootScope.$new();
Auth = _Auth_;
$controller("TestableCtrl", {
$scope: $scope,
Auth: Auth,
$stateParams: {}
});
});
});
describe("Controllers", function() {
beforeEach(module("moduleName"));
describe("Jasmine testableController", function () {
var scope,
controller,
uibModal;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
uibModal = { open: function() { return 'Whatever'; } }
controller = $controller('testableController', { $scope: scope, $uibModal: uibModal });
}));
it('should set the page title as "Welcome"', function () {
expect(scope.title).toBe('Welcome');
// you will probably do something like this at some point to verify
// that the modal gets opened on click or following any other action:
// var spy = spyOn(uibModal, 'open');
// expect(spy).toHaveBeenCalled();
});
});
});
angular.module('modulename').controller('nameofthecontroller', controller);