【发布时间】:2015-02-10 11:53:34
【问题描述】:
我正在为this directive 编写一个测试,并且我相信我做得正确,但是当我尝试访问isolateScope 时,我得到了未定义。
指令:
describe('directive: deposit-note', function() {
var expect = chai.expect;
var element,
scope,
$rootScope,
$compile,
$httpBackend;
beforeEach(module('directives.depositnote'));
beforeEach(inject(function (_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
}));
describe('with a filled-out transaction object', function() {
beforeEach(inject(function($injector) {
$httpBackend.whenGET('/templates/depositnote.tpl.html').respond(200, '');
scope = $rootScope.$new();
element = '<deposit-note checknumber="transaction.checkNumber" checktype="transaction.checkType.value" depositnote="transaction.depositNote" />';
scope.transaction = {
"checkNumber": "1234",
"checkType": "CASHIERS CHK",
"depositNote": "In Person"
};
element = $compile(element)(scope);
scope.$digest();
}));
it("should calculate the correct remaining characters", function() {
var isolated = element.isolateScope();
expect(isolated.totalRemaining).toBe(17);
});
});
});
当我逐步执行此操作时,我看到元素已编译,并且 chrome 的 devtools 显示 element.isolateScope() 确实是元素上的方法,但它返回未定义。
我错过了什么?
【问题讨论】:
-
标题中的函数名拼写错误:isolatedScope() 应该是isolateScope()。只是对像我这样的复制粘贴者的警告:-) >(刚刚修复)
标签: angularjs unit-testing chai