lundi 29 février 2016

how to load actual dependencies into a angularjs test with jasmine and karma

I have been looking around online, but seems, no one really injecting the actual dependencies into a unit test for angularjs using jasmine and karma...

I think there is definitely a separation of concern for the testing process, but I also would like to know how it integrated well with current dependencies in use... so just in case a dependencies is not working well with my component, I will be aware of it!

So, I wonder how can I inject the actual dependencies? So far I found online articles are all about mocking it with a fake one... But I want to use the actual one. Right now, when I enter karma start I am getting a error of Error: [$injector:unpr] Unknown provider: _Provider <- _ <-MyService

I inject services in forEach block like this beforeEach(angular.mock.inject(function(_MyService_) { I wonder if its because I am not using the fake service?

Any hint will help ia. Thanks!

describe('MyCtrl', function() {

  //Data Exposure Prep
  var $controller;
  var $rootScope;
  var $scope;
  var controller;
  var MyService;

  dd1 = {
    itinerary: globalMockData.d1,//I stored globalMockData somewhere else
  };

  beforeEach(angular.mock.module('myapp'));
  beforeEach(angular.mock.inject(function(_$rootScope_, _$controller_, _$httpBackend_) {
    $rootScope = _$rootScope_;
    $controller = _$controller_;
    $httpBackend = _$httpBackend_;
    $scope = $rootScope.$new();
    controller = $controller('MyCtrl', { $scope: $scope }, dd1);
  }));

  //BASIC INFO
  describe('should receive sth', function() {
    it('finds sth', function() {
      expect(controller.answer).toBeDefined();
    });
  });

});

`

Aucun commentaire:

Enregistrer un commentaire