javascript 如何为 Jasmine/Angular 创建一个助手来组合多个 beforeEach

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19563130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:59:18  来源:igfitidea点击:

How do I create a helper for Jasmine/Angular to combine multiple beforeEach's

javascriptangularjsjasminekarma-runner

提问by jhamm

I keep repeating some code in my spec file that injects a template and then compiles it. I extracted this code into a helper function to keep things DRY. I believe the problem is in trying to place to beforeEach's in a helper function. Here is the piece of my code that I am trying to abstract into a function:

我不断在我的规范文件中重复一些注入模板然后编译它的代码。我将此代码提取到辅助函数中以保持干燥。我相信问题在于试图将 to 放在beforeEach辅助函数中。这是我试图抽象为函数的一段代码:

  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();
  }));

Here is the helper function that I created:

这是我创建的辅助函数:

var setupTemplate = function(templateName, element) {
  beforeEach(module('app/views/' + templateName));
  beforeEach(inject(function($templateCache, $compile, $rootScope) {
    var template = $templateCache.get('app/views/' + templateName);
    $templateCache.put('views/' + templateName, template);
    var directive = angular.element(element);
    element = $compile(directive)($rootScope);
    $rootScope.$digest();
  }));

And now this is the call to the helper function:

现在这是对辅助函数的调用:

setupTemplate('header.html', '<clinical-header></clinical-header>');

At the end of my helper function, everything looks good, but when I jump to my itblock, everything is undefined. Can I extract multiple beforeEach's? What is the right way to do this? Also, where is the proper place to put jasmine helper functions and how is that done?

在我的辅助函数结束时,一切看起来都不错,但是当我跳转到我的it块时,一切都未定义。我可以提取多个beforeEach's 吗?这样做的正确方法是什么?另外,放置 jasmine 辅助函数的合适位置在哪里,如何完成?

回答by Eitan Peer

You can create global beforeEach() functions by writing them outside the context of a specific describe function. You should create a spec-helper.js class with these function and load it thru Karma config.

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

Note that the beforeEach functions will execute before any it function you are running (as they are global).

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

I created a fiddleto demonstrate, however the key with Karma is to add the file to the configuration so it is loaded by the browser.

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

Spec Helper:

规范助手:

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
});

Test Suite:

测试套件:

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);
  });
});