Javascript 执行单元测试时在angular指令中使用ng模型时出错
让我更具体一点。生产环境中的代码工作得很好(控制台中没有错误或异常)。然而,当我尝试进行测试时,它并没有很好地工作 这是我的指令模板的相关部分:Javascript 执行单元测试时在angular指令中使用ng模型时出错,javascript,angularjs,unit-testing,Javascript,Angularjs,Unit Testing,让我更具体一点。生产环境中的代码工作得很好(控制台中没有错误或异常)。然而,当我尝试进行测试时,它并没有很好地工作 这是我的指令模板的相关部分: <form ng-if="totalOrganizations > numOrganizationsForSearch" class="search"> <input type="text" class="form-control" placeholder="Search for..." ng-model
<form ng-if="totalOrganizations > numOrganizationsForSearch" class="search">
<input type="text" class="form-control" placeholder="Search for..." ng-model="searchText"/>
</form>
我可能在测试中遗漏了一些东西。如果条件ng if=“totalOrganizations>numOrganizationsForSearch”
不正确,因此它不会在我的指令DOM中添加输入
字段,则测试工作正常。但是,当此条件为真时,我的测试失败如下:
TypeError: document.createElement is not a function
at Object.hasEvent (dist/d16cd293c8e995aad0be/vendor.js:22558:34)
at baseInputType (dist/d16cd293c8e995aad0be/vendor.js:26751:17)
at textInputType (dist/d16cd293c8e995aad0be/vendor.js:26702:4)
at pre (dist/d16cd293c8e995aad0be/vendor.js:27292:63)
at invokeLinkFn (dist/d16cd293c8e995aad0be/vendor.js:13719:10)
at nodeLinkFn (dist/d16cd293c8e995aad0be/vendor.js:13192:12)
at compositeLinkFn (dist/d16cd293c8e995aad0be/vendor.js:12609:14)
at nodeLinkFn (dist/d16cd293c8e995aad0be/vendor.js:13208:25)
at compositeLinkFn (dist/d16cd293c8e995aad0be/vendor.js:12609:14)
at publicLinkFn (dist/d16cd293c8e995aad0be/vendor.js:12489:31)
Error: Declaration Location
at window.inject.angular.mock.inject (node_modules/angular-mocks/angular-mocks.js:2488:25)
at Suite.<anonymous> (spec/shared/portalSwitcher.spec.js:168:24)
似乎出于某种奇怪的原因,$document[0]
对象是空的。从阅读代码中可以看出,情况并非如此。我假设我的一些测试设置是错误的(这是我的第一个指令测试)。相关代码如下:
beforeEach(module('app.shared', function($provide) {
$provide.value('Service1', Service1);
//other dependencies
}));
beforeEach(function() {
//setup that makes the condition above return true
//and add ng-model to DOM.
});
beforeEach(inject(function($compile, $rootScope) { //it fails at this line
elm = $compile('<portal-switcher />')($rootScope.$new());
scope = elm.isolateScope();
scope.listPortals();
scope.$digest();
}));
beforeach(模块('app.shared',函数($provide)){
$PROFECT.value('Service1',Service1');
//其他依赖项
}));
beforeach(函数(){
//使上述条件返回真值的设置
//并将ng模型添加到DOM中。
});
beforeach(inject(function($compile,$rootScope){//它在这一行失败
elm=$compile(“”)($rootScope.$new());
scope=elm.isolateScope();
scope.listPortals();
范围。$digest();
}));
我试图用测试中的document
对象(它不是空对象,包含createElement
函数)在测试中提供$document
,但仍然存在相同的错误
我在Duckduckgo或Google中找不到任何类似的错误
我使用的是angular 1.4.9,angular mocks 1.4.9。我不认为这个问题与版本有关,但我在github回购协议上找不到任何问题
感谢您的帮助
function $SnifferProvider() {
this.$get = ['$window', '$document', function($window, $document) {
var eventSupport = {},
android =
toInt((/android (\d+)/.exec(lowercase(($window.navigator || {}).userAgent)) || [])[1]),
boxee = /Boxee/i.test(($window.navigator || {}).userAgent),
document = $document[0] || {}, //$document[0] is just an empty object here
//more code...
if (isUndefined(eventSupport[event])) {
var divElm = document.createElement('div'); //exception line. document object is empty and therefore doesn't have createElement function
eventSupport[event] = 'on' + event in divElm;
}
beforeEach(module('app.shared', function($provide) {
$provide.value('Service1', Service1);
//other dependencies
}));
beforeEach(function() {
//setup that makes the condition above return true
//and add ng-model to DOM.
});
beforeEach(inject(function($compile, $rootScope) { //it fails at this line
elm = $compile('<portal-switcher />')($rootScope.$new());
scope = elm.isolateScope();
scope.listPortals();
scope.$digest();
}));