Man*_*uuz 25 jasmine angularjs angular-ui-bootstrap karma-runner karma-jasmine
编辑:本文末尾的快速和肮脏的解决方案
我正在使用AngularUI-Bootstrap中的模态窗口,就像在网站上解释的那样,除了我分割文件.因此我有:
CallingController.js:
$scope.delete = function () {
if ($scope.selected.length > 0) {
// [...]
// preparing data
// [...]
var modalInstance = $modal.open({
templateUrl: 'views/modalView.html',
controller: 'modalCtrl',
resolve: {
itemArray: function () {
return $scope.selected;
}
}
});
modalInstance.result.then(function (confirm) {
if (confirm === true) {
// [...]
// treat
// [...]
}
});
}
};
Run Code Online (Sandbox Code Playgroud)
modalController.js:
myAppControllers.controller('modalCtrl',
function ($scope, $modalInstance, itemArray) {
$scope.accept = function () {
$modalInstance.close(true);
};
$scope.reject = function () {
$modalInstance.close(false);
};
$scope.itemArray = itemArray;
});
Run Code Online (Sandbox Code Playgroud)
当我用Karma测试此代码时(在karma配置文件中加载了ui-bootstrap-tpls.min.js文件),我收到以下错误: 错误:[$ injector:unpr] [ http:// errors. angularjs.org/1.2.15-build.2389+sha.c5f2f58/ $ injector/unpr?p0 =%24modalInstanceProvider%20%3C-%20%24modalInstance] 1 at Error(native),意思是jasmine无法管理找到$ modalInstance的提供者.
我甚至没有在这个控制器上测试东西,但还没有,但这是我的茉莉花测试文件:
testModalController.js:
describe('Controller: modalCtrl', function () {
beforeEach(module('myApp'));
var Ctrl;
var scope;
// Initialize the controller and a mock scope
beforeEach(inject(
function ($controller, $rootScope) {
scope = $rootScope.$new();
Ctrl = $controller('modalCtrl', { $scope: scope });
})
);
describe('Initial state', function () {
it('should instantiate the controller properly', function () {
expect(Ctrl).not.toBeUndefined();
});
it('should initialize its values properly', function () {
});
});
});
Run Code Online (Sandbox Code Playgroud)
你对这个问题有什么线索吗?它不是我使用(和测试)的第一个"外部"模块,我做了与其他模块相同的东西,除了这次它不起作用,我不知道为什么.
==========================================
编辑:快速和可能很脏的解决方案:
好的,基于Jasmine的控制器实例化中的范围模拟方法,我想出了如何"解决"我的问题,但它可能非常脏,所以如果你找到一个更好的方法来做我想做的事情,请随意发表评论.
testModalController.js:
describe('Controller: modalCtrl', function () {
beforeEach(module('myApp'));
var Ctrl;
var scope;
var modalInstance;
// Initialize the controller and a mock scope
beforeEach(inject(
function ($controller, $rootScope, _$modal_) {
scope = $rootScope.$new();
modalInstance = _$modal_.open({
templateUrl: 'views/modalView.html'
});
Ctrl = $controller('modalCtrl', {
$scope: scope,
$modalInstance: modalInstance,
itemArray: function () { return ['a', 'b', 'c']; }
});
})
);
describe('Initial state', function () {
it('should instantiate the controller properly', function () {
expect(Ctrl).not.toBeUndefined();
});
it('should initialize its values properly', function () {
});
});
});
Run Code Online (Sandbox Code Playgroud)
这样,Jasmine不再搜索提供程序,因为您已经注入了应该需要这些提供程序的项目.它有效,但我相信它可以以更好的方式完成......
Tom*_*cer 65
我只是通过创建模拟modal和modalInstance对象并验证它们是否已被我的控制器代码调用来解决这个问题.由于modal并且modalInstance是第三方库的一部分,测试它们是否正常工作并不是我们的责任 - 相反,我们有责任测试调用库的代码是否正常.
使用你的例子:
describe('Controller: modalCtrl', function () {
beforeEach(module('myApp'));
var Ctrl;
var scope;
var modalInstance;
// Initialize the controller and a mock scope
beforeEach(inject(
function ($controller, $rootScope) { // Don't bother injecting a 'real' modal
scope = $rootScope.$new();
modalInstance = { // Create a mock object using spies
close: jasmine.createSpy('modalInstance.close'),
dismiss: jasmine.createSpy('modalInstance.dismiss'),
result: {
then: jasmine.createSpy('modalInstance.result.then')
}
};
Ctrl = $controller('modalCtrl', {
$scope: scope,
$modalInstance: modalInstance,
itemArray: function () { return ['a', 'b', 'c']; }
});
})
);
describe('Initial state', function () {
it('should instantiate the controller properly', function () {
expect(Ctrl).not.toBeUndefined();
});
it('should close the modal with result "true" when accepted', function () {
scope.accept();
expect(modalInstance.close).toHaveBeenCalledWith(true);
});
it('should close the modal with result "false" when rejected', function () {
scope.reject();
expect(modalInstance.close).toHaveBeenCalledWith(false);
});
});
});
Run Code Online (Sandbox Code Playgroud)
这样,我们实际上不需要对Angular-UI对象有任何依赖,并且我们的单元测试很好并且是孤立的.
代替:
modalInstance = { // Create a mock object using spies
close: jasmine.createSpy('modalInstance.close'),
dismiss: jasmine.createSpy('modalInstance.dismiss'),
result: {
then: jasmine.createSpy('modalInstance.result.then')
}
};Run Code Online (Sandbox Code Playgroud)
这可以写成:
modalInstance = jasmine.createSpyObj('modalInstance', ['close', 'dismiss', 'result.then']);Run Code Online (Sandbox Code Playgroud)
现在还没有$ modalInstance $ uibModalInstance所以上面的每个"modalInstance"都应该替换为"uibModalInstance"
| 归档时间: |
|
| 查看次数: |
27006 次 |
| 最近记录: |