使用因缘不使用$范围测试控制器 [英] Testing a controller not using $scope using karma
本文介绍了使用因缘不使用$范围测试控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我也跟着在 docs.angularjs.org 的说明来测试业力控制器和它的工作完美。
不过,我想知道是否有可能测试未使用$范围控制器?
测试该控制器是确定的:
angular.module('starter.controllers')
.controller('PasswordController',功能PasswordController($范围)
{
$ scope.password ='';
$ scope.grade =功能()
{
变种大小= $ scope.password.length;
如果(大小→8){
$ scope.strength =强;
}否则如果(大小→3){
$ scope.strength ='媒介';
}其他{
$ scope.strength =弱;
}
};
});
不过,我想测试:
angular.module('starter.controllers')
.controller('PasswordController',功能PasswordController()
{
VAR = myController的这一点;
myController.password ='';
myController.grade =功能()
{
VAR大小= myController.password.length;
如果(大小→8){
myController.strength =强;
}否则如果(大小→3){
myController.strength ='媒介';
}其他{
myController.strength =弱;
}
};
});
测试code是以下内容:
描述('PasswordController',函数()
{
beforeEach(模块('starter.controllers')); 变量$控制器; beforeEach(注入(函数($ _ _控制器){
$控制器= _ $ controller_;
})); 描述('$ scope.grade',函数(){
变量$范围,控制器; beforeEach(函数(){
$范围= {};
//这行不与第二控制器的工作
控制器= $控制器('PasswordController',{$范围:$范围});
}); 它('设置实力强,如果密码的长度→8个字符',函数(){
$ scope.password ='longerthaneightchars';
$ scope.grade();
期待($ scope.strength).toEqual(强);
});
});
});
解决方案
这应该工作:
描述('PasswordController',函数()
{
beforeEach(模块('starter.controllers')); 变量$控制器; beforeEach(注入(函数($ _ _控制器){
$控制器= _ $ controller_;
})); 描述('$ scope.grade',函数(){
VAR控制器; beforeEach(函数(){
控制器= $控制器('PasswordController',{});
}); 它('设置实力强,如果密码的长度→8个字符',函数(){
controller.password ='longerthaneightchars';
controller.grade();
期待(controller.strength).toEqual(强);
});
});
});
I followed the instructions on docs.angularjs.org to test a controller with karma and it's perfectly working. However I was wondering if it's possible to test a controller which is not using $scope?
Testing this controller is ok:
angular.module('starter.controllers')
.controller('PasswordController', function PasswordController($scope)
{
$scope.password = '';
$scope.grade = function()
{
var size = $scope.password.length;
if (size > 8) {
$scope.strength = 'strong';
} else if (size > 3) {
$scope.strength = 'medium';
} else {
$scope.strength = 'weak';
}
};
});
But I'd like to test:
angular.module('starter.controllers')
.controller('PasswordController', function PasswordController()
{
var myController = this;
myController.password = '';
myController.grade = function()
{
var size = myController.password.length;
if (size > 8) {
myController.strength = 'strong';
} else if (size > 3) {
myController.strength = 'medium';
} else {
myController.strength = 'weak';
}
};
});
The testing code is the following:
describe('PasswordController', function()
{
beforeEach(module('starter.controllers'));
var $controller;
beforeEach(inject(function(_$controller_){
$controller = _$controller_;
}));
describe('$scope.grade', function() {
var $scope, controller;
beforeEach(function() {
$scope = {};
//This line doesn't work with the second controller
controller = $controller('PasswordController', { $scope: $scope});
});
it('sets the strength to "strong" if the password length is >8 chars', function() {
$scope.password = 'longerthaneightchars';
$scope.grade();
expect($scope.strength).toEqual('strong');
});
});
});
解决方案
This should work:
describe('PasswordController', function()
{
beforeEach(module('starter.controllers'));
var $controller;
beforeEach(inject(function(_$controller_){
$controller = _$controller_;
}));
describe('$scope.grade', function() {
var controller;
beforeEach(function() {
controller = $controller('PasswordController', {});
});
it('sets the strength to "strong" if the password length is >8 chars', function() {
controller.password = 'longerthaneightchars';
controller.grade();
expect(controller.strength).toEqual('strong');
});
});
});
这篇关于使用因缘不使用$范围测试控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文