繁体   English   中英

单元测试角度指令包装slickgrid

[英]Unit testing an angular directive wrapping slickgrid

我想测试我的角度指令,该指令是slickgrid的包装器。

'use strict';
describe('Unit: Grid Directive', function() {
  var $scope;
  var element;

  beforeEach(module('grid'));
  beforeEach(inject(function($compile, $rootScope) {
    $scope = $rootScope;    
    element = angular.element('<grid class="slickgrid-table" id="miscGrid" query="query"/>');
    $scope.query = { symbol: 'AAAA' };
    $compile(element)($scope);
    $scope.$digest();
  }));

问题是slickgrid使用jquery来查找应该将其插入网格的ID。

Error: SlickGrid requires a valid container, #miscGrid does not exist in the DOM.

我的问题是,我怎样才能使它正常工作? 我如何“欺骗” slickgrid以了解我要编译的元素是有效的容器?

在大多数情况下,我发现将元素附加到DOM而不是欺骗任何东西,这比欺骗任何东西都容易。 根据项目githithub中的示例,无论如何,控件都必须位于DOM中才能正确初始化( http://mleibman.github.io/SlickGrid/examples/example-explicit-initialization.html

我倾向于将以下行放在任何指令规范中

var sandbox;
beforeEach(function() { sandbox = angular.element('<div>'); angular.element(document.body).append(sandbox); });
afterEach(function() { sandbox.remove(); sandbox = null; });

然后,只需在编译之前将指令附加到沙箱即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM