vendredi 5 août 2016

How to fix the unit test issue in my case?

I am trying to unit test two functions codes and keep getting error of undefined object.

my controller

vm = this;
//always fire first in the app
vm.getCompany = function() {
    api.getCompany(function(res){        
        //do stuff
    })
}

//always fire second in the app
vm.getEmployee = function() {
    api.getEmployee(function(res){
        //do stuff
    })
}

api service

var company;

function getCompany() {
    $http.get(url).then(function(comp){
          company = comp;
          return company;
    })
}

function getEmployee = function() {
    var name = company.name
    $http.get(url).then(function(employee){
        // do stuff    
    }
}

unit test.

beforeEach(function(){
   module(‘myApp);
        inject(function ($injector) {
            $controller = $injector.get('$controller');
            $rootScope = $injector.get('$rootScope');
            $scope = $rootScope.$new();
            $httpBackend = $injector.get('$httpBackend');
            api = $injector.get('api');
        });

         vm = $controller'myCtrl', {
            $scope : $scope
        });

})

describe (‘test’, function(){
    it(‘should get company’, function(){
         vm.getCompany();
         $httpBackend.flush();
         // stuff and works
    })
    it(‘should get Employee’, function(){
        vm.getEmployee()
        $httpBackend.flush();
        //getting error says 
        //'undefined' is not an object (evaluating 'company.name’)
    })
})

I am getting 'undefined' is not an object (evaluating 'company.name’) under getEmployee function in service.

I have tried many different ways but still not sure how to fix it, can someone help me about it? Thanks!

Aucun commentaire:

Enregistrer un commentaire