$ rootscope.digest在角指令单元测试中投掷“不再请求的请求”
-
20-12-2019 - |
题
我已经编写了一个指令,检查某些权限,如果权限是ko,则删除来自DOM的元素。
我很乐意进行单位测试,但是......下摆,我正在敲打我的头部墙壁,使这个简单的测试工作。
我使用$ rootscope.digest()编译一块HTML。调用此功能时,角度试图加载我的应用主页,我得到了可怕的“不再请求预期的”错误。
所以这里是测试:
describe('Unit testing permission-needed', function() {
var $compile;
var $rootScope;
var $httpBackend;
// Load the myApp module, which contains the directive
beforeEach(module('app'));
beforeEach(angular.mock.module('ngMockE2E'));
beforeEach(inject(function(_$compile_, _$rootScope_, $injector) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$httpBackend = $injector.get('$httpBackend'); // not sur if I can inject it like others services ?
$httpBackend.whenGET('app/login/login.tpl.html').passThrough(); // this doesn't seem to work
}));
it('should replace the element with the appropriate content', function() {
// Compile a piece of HTML containing the directive
var element = $compile("<div permission-needed><span>Some content goes here</span></div>")($rootScope);
$rootScope.$digest(); // BAM => "no more request expected"
// Do the test here
// expect(....);
});
});
.
请注意,如果我使用 。响应('这里的某些HTML'); 代替 .passthrough()它有效。
谢谢。
解决方案
好吧,回答自己:
使用$ new()rootscope,测试通过:
$rootScope = _$rootScope_.$new();
.
希望这有助于某人。
不隶属于 StackOverflow