title: Setting global variables in jasmine for angularjs
body: <p>I have an angular application with some global environment variables defined in an env.js file:</p>
  
  <pre><code>(function(sp) {
  'use strict';
  
  pk.env = pk.env || {};
  
  // localhost
  pk.env.baseUrl = 'http://localhost:8080/';
  })(typeof exports === 'undefined' ? (this.pk = this.pk || {}) : exports);
  </code></pre>
  
  <p>These variables are used in multiple factories to make REST API calls:</p>
  
  <pre><code>'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;
  }
  </code></pre>
  
  <p>I am writing unit tests for this factory using Jasmine and I keep getting the error: </p>
  
  <blockquote>
    <p>ReferenceError: Can't find variable: pk</p>
  </blockquote>
  
  <p>If I remove this variable reference from the factory, the tests run fine.</p>
  
  <pre><code>'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();
    });
  })
  </code></pre>
  
  <p>How do I inject value of 'pk.env.baseUrl' into the factory? I have tried using $window, but it didn't work. </p>