javascript 在 jasmine 中为 angularjs 设置全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30877189/
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-28 12:52:03  来源:igfitidea点击:

Setting global variables in jasmine for angularjs

javascriptangularjsjasminekarma-jasmine

提问by imgr8

I have an angular application with some global environment variables defined in an env.js file:

我有一个 angular 应用程序,在 env.js 文件中定义了一些全局环境变量:

(function(sp) {
'use strict';

pk.env = pk.env || {};

// localhost
pk.env.baseUrl = 'http://localhost:8080/';
})(typeof exports === 'undefined' ? (this.pk = this.pk || {}) : exports);

These variables are used in multiple factories to make REST API calls:

这些变量在多个工厂中用于进行 REST API 调用:

'use strict';

angular.module('pkApp').factory('pkFactory', PKFactory);

function PKFactory($http) {
    var urlBase = pk.env.baseUrl;
    var apiUrl = 'v1/data';
    var _pkFactory = {};

    _pkFactory.getData = function() {
        return $http.get(urlBase + apiUrl);
    };


    return _pkFactory;
}

I am writing unit tests for this factory using Jasmine and I keep getting the error:

我正在使用 Jasmine 为这家工厂编写单元测试,但我不断收到错误消息:

ReferenceError: Can't find variable: pk

参考错误:找不到变量:pk

If I remove this variable reference from the factory, the tests run fine.

如果我从工厂中删除此变量引用,则测试运行良好。

'use strict';

console.log('=== In pk.factory.spec');

describe('Unit: pkFactory', function() {

  beforeEach(module("pkApp"));

  var $httpBackend, $rootScope, pkFactory;

  beforeEach(inject(function($injector) {
    // Set up the mock http service responses
    $httpBackend = $injector.get('$httpBackend');

    $httpBackend.when('GET', 'v1/data').respond('Not found');

    pkFactory = $injector.get('pkFactory');

  }));

  afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });

  it('expects getData method to be defined', function(){
    expect(pkFactory.getData()).toBeDefined();
    $httpBackend.flush();
  });
})

How do I inject value of 'pk.env.baseUrl' into the factory? I have tried using $window, but it didn't work.

如何将“pk.env.baseUrl”的值注入工厂?我曾尝试使用 $window,但没有奏效。

回答by vvondra

You should avoid using the globals in Angular completely.

您应该完全避免在 Angular 中使用全局变量。

Convert the file to an angular value or constant:

将文件转换为角度值或常量:

angular.module('pkApp').value('pk', pk);

now you can change pkFactoryto get the pkobject injected

现在您可以更改pkFactorypk注入对象

function PKFactory($http, pk) {
    // pk is no longer from global scope, but injected from angular as an argument
    var urlBase = pk.env.baseUrl;
    var apiUrl = 'v1/data';
    var _pkFactory = {};

    _pkFactory.getData = function() {
        return $http.get(urlBase + apiUrl);
    };


    return _pkFactory;
}

and in tests, you can now mock the pk to a different value (or not do anything and use the one from code)

在测试中,您现在可以将 pk 模拟为不同的值(或不做任何事情并使用代码中的值)

回答by Matthias

As pretty much already answered here, you can also declare a global variable within your test file

正如这里已经回答的差不多,您还可以在测试文件中声明一个全局变量

var globalVar = "something";

describe('Your test suit', function() {
    ...
});

and if you are using Karma you can edit the karma.conf.jsfile to define it

如果您使用的是 Karma,您可以编辑karma.conf.js文件来定义它

 // list of files / patterns to load in the browser
 files: [
    ...,
    'file-containing-the-global-variable.js'
 ],