Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用角度测试指令_Javascript_Html_Angularjs_Unit Testing_Mocha.js - Fatal编程技术网

Javascript 用角度测试指令

Javascript 用角度测试指令,javascript,html,angularjs,unit-testing,mocha.js,Javascript,Html,Angularjs,Unit Testing,Mocha.js,我有一个angular指令,动态创建5个按钮。我目前正在编写单元测试,我决定编写一个测试所有按钮是否都存在的单元测试。然而,我似乎无法通过考试。 这是我的一些代码 Test.js: beforeEach(inject(function($compile,$rootScope,$location,$httpBackend,$route) { location = $location; scope = $rootScope.$new(); route = $rou

我有一个angular指令,动态创建5个按钮。我目前正在编写单元测试,我决定编写一个测试所有按钮是否都存在的单元测试。然而,我似乎无法通过考试。 这是我的一些代码

Test.js:

    beforeEach(inject(function($compile,$rootScope,$location,$httpBackend,$route) {

    location = $location;
    scope = $rootScope.$new();

    route = $route;

    $httpBackend.whenGET('partials/options/options-skeleton.html')
                .respond(200);
    elm = angular.element("<options></options>");


    httpBackend = $httpBackend;
    $compile(elm)(scope);
    scope.$digest();

}));
describe('initial value test', function(){

    it('should have 5 buttons', function(){
        var options = elm.find('div');
        var innerElm = options.find('div');
        expect(innerElm.length).to.equal(5);
    });
});
我曾尝试在测试中对var选项使用JSON.stringify()来查看它设置为什么元素,但我一直收到一个错误:

TypeError: JSON.stringify cannot serialize cyclic structures.  
我们将非常感谢您的任何帮助。如果需要更多的代码,请告诉我,我会更新我的问题。提前谢谢

更新:

我试过注销

console.log(angular.element("<options></options>"));

将导致指令被初始化,并使日志输出通过指令加载的templateUrl。

以及
options.optionsArray
如何进入指令?您似乎从未在测试中定义它,因此
ng repeat
没有什么可重复的,因此它不会创建
  • s。好的,我更新了我的问题。所以现在我寻找嵌套在另一个中的。无论是否重复运行,都会创建它们。当我运行上述代码时,我仍然得到“预期0等于5”。我不知道是否达到了您的指令html,因为您已经通过
    $httpBackend.whenGET('partials/options/options skeleton.html')模拟了对模板的请求。respond(200)是,这似乎是一个可能的解释。我将尝试html2js和$templateCache,看看这是否有帮助。不管怎样,我都会更新这个问题。
    
    TypeError: JSON.stringify cannot serialize cyclic structures.  
    
    console.log(angular.element("<options></options>"));
    
    LOG: {0: <options></options>, length: 1}
    
    $compile(elm)(scope);