Including dependencies does not work in angular / browsify / karma / jasmine unit test

I use Browserify in conjunction with Angular and testing with Karma and Jasmine. I am trying to test a factory function that uses $ http. Here's a look at the factory:

module.exports = function($http) {
  var messageFactory = {};
  messageFactory.fetchAll = function(url) {
    $http.get(url).success(function() {
      console.log('success');
    });
  }

  return messageFactory
}

Here's the test:

var angular = require('angular');
var messageFactoryModule = require('./message.factory.js');
require('angular-mocks');


describe('Factory: Message', function() {
  var messageFactory;
  var $httpBackend;
  var url;

  beforeEach(inject(function(_$httpBackend_) {
    $httpBackend = _$httpBackend_;
    messageFactory = new messageFactoryModule();
    url = 'http://example.com';
  }));

  describe('when fetch is called', function() {
    it('should fetch messages from the database', function() {
      $httpBackend.expectGET(url)
        .respond({});

      messageFactory.fetchAll(url);

      $httpBackend.flush();
    });    
  });
});

When I run the test, I get an error because $ http is not defined in the factory

TypeError: Cannot read property 'get' of undefined

I also get errors when I try to implement other services, such as $ filter, and see if this is defined. When I actually run Message.fetchAll in an Angular app, everything works fine. Obviously, something is wrong with my test setup, I just can't figure that out.

thank

+4
1

beforeEach :

  beforeEach(inject(function(_$httpBackend_, $http) {
    $httpBackend = _$httpBackend_;
    messageFactory = new messageFactoryModule($http);
    url = 'http://example.com';
  }));

, $http . 100% , , , .

+3

All Articles