简体   繁体   English

在angularjs中为jasmine设置全局变量

[英]Setting global variables in jasmine for angularjs

I have an angular application with some global environment variables defined in an env.js file: 我有一个角度应用程序,在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 ReferenceError:找不到变量: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? 如何将'pk.env.baseUrl'的值注入工厂? I have tried using $window, but it didn't work. 我试过使用$ window,但它没有用。

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 pkFactory to get the pk object injected 现在您可以更改pkFactory以获取注入的pk对象

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模拟为不同的值(或者不做任何事情并使用代码中的那个)

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.js file to define it 如果您使用的是Karma,您可以编辑karma.conf.js文件来定义它

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

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

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