sjt*_*003 7 jasmine angularjs angularjs-directive
使用Angular v1.2.25和rails资产管道,我试图测试指令的隔离范围确实已经更新.由于isolateScope()返回undefined我得到预期未定义的定义...'
describe("cool directive", function() {
beforeEach(module('necessaryModule'));
var scope, $rootScope, $compile, elem,
baseElement = '<div auto="mock_a" inc="mock_p" method="mock_m" reset-method="mock_r"></div>';
beforeEach(inject(function( _$rootScope_, _$compile_, _$httpBackend_, $http){
$compile = _$compile_;
$rootScope = _$rootScope_;
scope = $rootScope.$new();
angular.extend(scope, {
mock_a: [
{name: "example1"},
{name: "example2"}
],
mock_m: function(){
return $http.get('/mockBackend', {
params:{
page: scope.mockPage
}
});
},
mock_r: function() {
scope.page = 1;
scope.list = [];
load();
},
mock_p: 1
});
$httpListGet = _$httpBackend_;
$httpListGet.whenPOST('/api/something').respond({});
$httpListGet.whenGET('/mockBackend').respond({name: "example3"});
$httpListGet.whenGET('/mockBackend?page=1').respond({name: "example3"});
$httpListGet.whenGET('/mockBackend?page=2').respond({name: "example4"});
}));
var create = function() {
elem = angular.element(baseElement);
compiledElement = $compile(elem)(scope);
elem.scope().$apply();
return compiledElement;
};
it("has 'list' defined", function() {
var compiledElem = create();
var isolateElemScope = compiledElem.isolateScope();
$rootScope.$apply();
console.log('isolateElemScope',isolateElemScope);
expect(isolateElemScope.list).toBeDefined();
});
Run Code Online (Sandbox Code Playgroud)
我期望指令范围是可访问和可测试的,但是当我测试它时,我得到了未定义.谢谢.
guy*_*abi 12
获取isolateScope我使用以下代码
compiledElem.children().scope()
Run Code Online (Sandbox Code Playgroud)
这是因为大多数指令不使用replace,这意味着directive标记在页面上,并且directive implementation作为该标记的子项添加.在这种情况下,隔离范围将属于子项.
即使不是这种情况,代码片段仍应有效 - 因为孩子们将分享父母的范围.
唯一不起作用的情况是,你有2个嵌套指令的极端情况,其中内部指令使用replace.但我从未见过这个.