AngularJS 和 Jasmine:isolateScope() 未定义
AngularJS and Jasmine: isolateScope() is undefined
经过两天的研究和测试,我需要寻求帮助。
我正在尝试使用 Jasmine 测试指令,但我不想包含 Karma 引擎。
为了看测试结果,我使用了jasmine-html库,jasminecss.
我能够轻松地测试服务,但是当涉及到测试指令时,我无法访问隔离范围。要记住的一件事是我不想在我的指令中使用控制器,而是 link 函数。
(根据 Angular doc,仅当您想将 API 暴露给其他指令时才应使用控制器。)
我在 Whosebug 上找到了多个答案,但其中 none 个有效。
我尝试的最后一件事是基于 this 回答的问题 Whosebug。
这是我要复制的测试
describe('Wikis Directive Test Suite', function () {
var $scope, scope, elem, directive, linkFn, html;
beforeEach(module('app'));
beforeEach(function () {
html = '<wikis></wikis>';
inject(function ($compile, $rootScope, $templateCache) {
$templateCache.put('templates/wiki-list.html', '<div>wiki template</div>');
$scope = $rootScope.$new();
$scope.wikis = [];
elem = angular.element(html);
elem= $compile(elem)($scope);
//scope = elem.isolateScope(); /*This is always undefined!*/
scope = elem.scope(); /* this doesn't have addWiki, only the empty wikis array */
$rootScope.$digest();
});
});
it('add Wiki should add a valid wiki URL to artist', function () {
var url = 'http://www.foo.com';
scope.newWikiURL = url;
scope.addWiki();
expect(scope.wikis.length).toBe(1);
expect(scope.wikis[0]).toBe(url);
expect(scope.newWikiURL).toBe('');
});
});
"only" 的不同之处在于,我需要使用 Jasmine 2.4.1 和 Angular 1.0.7。不幸的是,这些库似乎无法进行测试。
问题是 isolateScope() 总是未定义的!
我创建了一个 plunker 来重现这个问题。
好吧,我(其实是我的一个同事发现的)我做错了什么!
这是有效的正确代码。
describe('Wikis Directive Test Suite', function () {
var $scope, scope, elem, directive, linkFn, html;
beforeEach(module('app'));
beforeEach(function () {
html = '<wikis></wikis>';
inject(function ($compile, $rootScope, $templateCache) {
$templateCache.put('templates/wiki-list.html', '<div>wiki template</div>');
$scope = $rootScope.$new();
$scope.wikis = [];
elem = angular.element(html);
elem = $compile(elem)($scope);
$scope.$digest();
scope = elem.isolateScope();
});
});
it('add Wiki should add a valid wiki URL to artist',inject( function () {
var url = 'http://www.foo.com';
scope.newWikiURL = url;
scope.addWiki();
expect(scope.wikis.length).toBe(1);
expect(scope.wikis[0]).toBe(url);
expect(scope.newWikiURL).toBe('');
}));
});
经过两天的研究和测试,我需要寻求帮助。 我正在尝试使用 Jasmine 测试指令,但我不想包含 Karma 引擎。
为了看测试结果,我使用了jasmine-html库,jasminecss.
我能够轻松地测试服务,但是当涉及到测试指令时,我无法访问隔离范围。要记住的一件事是我不想在我的指令中使用控制器,而是 link 函数。 (根据 Angular doc,仅当您想将 API 暴露给其他指令时才应使用控制器。)
我在 Whosebug 上找到了多个答案,但其中 none 个有效。
我尝试的最后一件事是基于 this 回答的问题 Whosebug。
这是我要复制的测试
describe('Wikis Directive Test Suite', function () {
var $scope, scope, elem, directive, linkFn, html;
beforeEach(module('app'));
beforeEach(function () {
html = '<wikis></wikis>';
inject(function ($compile, $rootScope, $templateCache) {
$templateCache.put('templates/wiki-list.html', '<div>wiki template</div>');
$scope = $rootScope.$new();
$scope.wikis = [];
elem = angular.element(html);
elem= $compile(elem)($scope);
//scope = elem.isolateScope(); /*This is always undefined!*/
scope = elem.scope(); /* this doesn't have addWiki, only the empty wikis array */
$rootScope.$digest();
});
});
it('add Wiki should add a valid wiki URL to artist', function () {
var url = 'http://www.foo.com';
scope.newWikiURL = url;
scope.addWiki();
expect(scope.wikis.length).toBe(1);
expect(scope.wikis[0]).toBe(url);
expect(scope.newWikiURL).toBe('');
});
});
"only" 的不同之处在于,我需要使用 Jasmine 2.4.1 和 Angular 1.0.7。不幸的是,这些库似乎无法进行测试。 问题是 isolateScope() 总是未定义的!
我创建了一个 plunker 来重现这个问题。
好吧,我(其实是我的一个同事发现的)我做错了什么!
这是有效的正确代码。
describe('Wikis Directive Test Suite', function () {
var $scope, scope, elem, directive, linkFn, html;
beforeEach(module('app'));
beforeEach(function () {
html = '<wikis></wikis>';
inject(function ($compile, $rootScope, $templateCache) {
$templateCache.put('templates/wiki-list.html', '<div>wiki template</div>');
$scope = $rootScope.$new();
$scope.wikis = [];
elem = angular.element(html);
elem = $compile(elem)($scope);
$scope.$digest();
scope = elem.isolateScope();
});
});
it('add Wiki should add a valid wiki URL to artist',inject( function () {
var url = 'http://www.foo.com';
scope.newWikiURL = url;
scope.addWiki();
expect(scope.wikis.length).toBe(1);
expect(scope.wikis[0]).toBe(url);
expect(scope.newWikiURL).toBe('');
}));
});