Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.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 如何为Jasmine/Angular创建一个辅助对象,以组合多个beforeach';s_Javascript_Angularjs_Jasmine_Karma Runner - Fatal编程技术网

Javascript 如何为Jasmine/Angular创建一个辅助对象,以组合多个beforeach';s

Javascript 如何为Jasmine/Angular创建一个辅助对象,以组合多个beforeach';s,javascript,angularjs,jasmine,karma-runner,Javascript,Angularjs,Jasmine,Karma Runner,我一直在重复我的spec文件中的一些代码,这些代码注入了一个模板,然后对其进行编译。我将这段代码提取到一个helper函数中,以保持干燥。我认为问题在于如何在助手函数中的每个之前放置到。下面是我试图抽象为函数的代码片段: beforeEach(module('app/views/header.html')); beforeEach(inject(function($templateCache, $compile, $rootScope) { template = $templa

我一直在重复我的spec文件中的一些代码,这些代码注入了一个模板,然后对其进行编译。我将这段代码提取到一个helper函数中,以保持干燥。我认为问题在于如何在助手函数中的每个之前放置到
。下面是我试图抽象为函数的代码片段:

  beforeEach(module('app/views/header.html'));

  beforeEach(inject(function($templateCache, $compile, $rootScope) {
    template = $templateCache.get('app/views/header.html');
    $templateCache.put('views/header.html', template);
    var directive = angular.element('<clinical-header></clinical-header>');
    element = $compile(directive)($rootScope);
    $rootScope.$digest();
  }));
setupTemplate('header.html', '<clinical-header></clinical-header>');
现在是对helper函数的调用:

  beforeEach(module('app/views/header.html'));

  beforeEach(inject(function($templateCache, $compile, $rootScope) {
    template = $templateCache.get('app/views/header.html');
    $templateCache.put('views/header.html', template);
    var directive = angular.element('<clinical-header></clinical-header>');
    element = $compile(directive)($rootScope);
    $rootScope.$digest();
  }));
setupTemplate('header.html', '<clinical-header></clinical-header>');
setupTemplate('header.html','';

在helper函数的末尾,一切看起来都很好,但是当我跳转到
it
块时,一切都是未定义的。我可以在每个
之前提取多个?正确的方法是什么?另外,jasmine helper函数的正确放置位置是哪里?如何放置?

您可以通过在特定描述函数的上下文之外编写全局beforeach()函数来创建这些函数。 您应该使用这些函数创建spec-helper.js类,并通过Karma配置加载它

请注意,beforeach函数将在运行的任何it函数之前执行(因为它们是全局函数)

我创建了一个示例来演示,但是Karma的关键是将文件添加到配置中,以便由浏览器加载

等级库辅助对象:

var myGlobal;
beforeEach(function() {
    // This will run before any it function.
    // Resetting a global state so the change in this function is testable
   myGlobal = 10
});
测试套件:

describe('first suite', function(){
   it('is a test', function(){
     expect(myGlobal).toBe(10);
     // Reset the value to show that beforeEach is executed for each it function
     myGlobal = 20;
     expect(myGlobal).toBe(20);
  });

  it('is another test', function($location){
     expect(myGlobal).toBe(10);
     myGlobal = 20;
     expect(myGlobal).toBe(20);
  });
});

如果您只想运行测试子集的代码,而测试在单独的文件中,那么这并不能解决问题。您是对的。这适用于所有测试,不管它们的文件是什么。我的项目中有一个非常类似的情况。在每次调用之前,我将我的全局
包装在一个函数中,只需在我的
描述
块顶部调用该函数,就可以调用任何需要它的测试套件。如何将该文件添加到您的Karma配置中?当我尝试它时,它失败得很厉害,每次测试都会出现异常:“错误:Injector已经创建,无法注册模块!”@Isiden在调用Injector后,您不能调用angular.mock.module函数。您必须在第一次调用inject()之前完成对module()的所有调用,因此,如果您的全局Beforeach调用inject(),则无法在测试中调用module(),即-不要在全局Beforeach中调用inject()