0

The app is working properly with my module. But building the following mock returns :

$global_vars is not defined

I imagine that I'm not injecting the module properly. What am I missing here?

describe('CalendarController', function() {
  beforeEach(module("$global_vars"));
  beforeEach(inject(function($rootScope, $controller, $location, $injector, $global_vars) {
    var $httpBackend;
    $httpBackend = $injector.get('$httpBackend');
    return this.controller = $controller(CalendarController, {
      $scope: this.scope,
      $location: $location,
      $global_vars: $global_vars
    });
  }));
  return it("can be instantiated", function() {
    return expect($global_vars).not.toBeNull();
  });
});

note: this is a coffeescript translation to vanilla js

UPDATE

Ok so by doing this I get another, possibly better error?

Error: Unknown provider: $httpProvider <- $http <- $global_vars

.

describe 'CalendarController', ->

  beforeEach module("$global_vars")
  beforeEach module("GlobalService")
  beforeEach inject ($rootScope, $controller, $location, $injector) ->
    $injector = angular.injector [ '$global_vars' ]
    $global_vars = $injector.get('$global_vars')

    $global_vars = $injector.get('$global_vars')
    @controller = $controller CalendarController, {$scope: @scope, $location: $location}

The bare bones of my backbone.js

angular.module("VarsService", []).factory "$global_vars", ["$http", '$location', ($http, $location) ->  
  global_vars.get_calendar = ->
     console.log 'blam!'
Trip
  • 26,756
  • 46
  • 158
  • 277
  • 1
    Is `$global_vars` the name of the module or the name of the service or both? – Josh David Miller Feb 14 '13 at 05:47
  • Both, is that a problem? – Trip Feb 14 '13 at 13:35
  • Where do you have `$global_vars` declared? Is it available in your `describe` function scope? – zbynour Feb 14 '13 at 16:32
  • @zbynour I believe that's the root of the issue is that when I instantiate $global_vars its merely adding it to my mock controller, but not making it accessible within the Jasmine test. This seemed like the closest way to do that : http://stackoverflow.com/questions/13013772/how-do-i-test-an-angularjs-service-with-jasmine But unfortunately that doesn't allow me to access the module. As in, calling `$global_vars` returns `undefined` – Trip Feb 14 '13 at 16:53
  • No, it's not a problem, but I wanted to make sure it wasn't a typo. Can you post your `$global_vars` module code? – Josh David Miller Feb 14 '13 at 18:57
  • Hey Josh, I did actually change it to have a different name between my Service and its Factory. After the name change, the app is running functionaly, but the tests still doesn't allow me to access `$global_vars`. – Trip Feb 14 '13 at 19:18
  • Possible duplicate of [Angular mock fails to inject my module dependencies](http://stackoverflow.com/questions/23382207/angular-mock-fails-to-inject-my-module-dependencies) – Paul Sweatte Dec 13 '15 at 23:18

0 Answers0