2016-09-13 46 views
0

我已经写了一个角度测试应用程序。试着用它学习测试。测试Angularjs控制器服务作为依赖时的困难

控制器看起来像这样

var EmployeeModule = EmployeeModule || {}; 

(function (currentModule) { 
    EmployeeModule.EmployeeController = function ($scope, $routeParams, $location, Employees) { 
     this.RegEmployees = Employees.GetEmployees(); 
     this.currentEmployee = {}; 
     if ($routeParams.Id != undefined) { 

      console.log("to check $route"+$routeParams); 
      this.currentEmployee = Employees.GetEmployeeById($routeParams.Id); 
     } 
     this.Id = ""; 
     this.firstname = ""; 
     this.lastname = ""; 
     this.email = ""; 
     this.dob = ""; 
     this.department = ""; 
     this.RegisterEmployee = function() { 
      Employees.RegisterEmployee(this.firstname, this.lastname, this.email, this.dob, this.department); 
      if ($scope.employeeForm.$valid) 
       $location.path("/Employees"); 
     } 
EmployeeModule.Employee = function (firstname, lastname, email, dob, department) { 
     this.Id = Date.now(); 
     this.FirstName = firstname; 
     this.LastName = lastname; 
     this.Email = email; 
     this.DOB = dob; 
     this.Department = department; 
    } 

})(EmployeeModule); 

和我的角度服务看起来这

var app = angular.module('angularApp2App'); 
app.factory('Employees', function() { 
    var RegEmployees = {}; 
    var Employees = []; 
    //Service 
    RegEmployees.RegisterEmployee = function (firstname, lastname, email, dob, department) { 
     var newEmployee = new EmployeeModule.Employee(firstname, lastname, email, dob, department); 

     Employees.push(newEmployee); 

    } 
return RegEmployees; 
}); 
app.controller('EmployeeController', ['$scope', '$routeParams', '$location', 'Employees', EmployeeModule.EmployeeController]); 

我想要编写单元测试控制器,但不知道,我怎么能注入服务为单元测试中对控制器的依赖性。

请在此 帮助和我的单元测试代码是

describe('Controller: EmployeeController', function() { 

    // load the controller's module 
    beforeEach(module('angularApp2App')); 

    var empCtrl, 
     scope; 

    // Initialize the controller and a mock scope 
    beforeEach(inject(function ($controller, $rootScope, $injector) { 
     scope = $rootScope.$new(); 
     var employeeService = $injector.get('Employees'); 
     console.log(employeeService); 
     spyOn(employeeService , 'RegisterEmployee'); 
     empCtrl = $controller('EmployeeController', { 
      $scope: scope 
       // place here mocked dependencies 
     }); 
    })); 


    it('checking default values', function() { 
     expect(empCtrl.currentEmployee).toEqual({}); 
    }); 

    it('checking RegisterEmployee Method',function() { 
     expect(RegisterEmployee).toHaveBeenCalled(); 
    }); 
}); 

感谢

+0

请指出你的问题中单元测试的当前代码。 – estus

+0

更新测试逻辑,但不知道如果这是正确的做法 – Geeky

回答

0

基本上你只需要注入service当你想嘲笑或测试的东西作为号召服务。如果情况并非如此,角将自动为您注入。我会在这里写一个小测试,以帮助你了解一个洞察:

describe('Controller: EmployeeController', function() { 

    // load the controller's module 
    beforeEach(module('angularApp2App')); 

    it('Check that Employees.GetEmployees was called on startup', inject(function ($controller, $rootScope, Employees) { 
     //Here I am spying on Employees.GetEmployees. It means that the call 
     //to this method will be replaced with an empty function(){} 
     //(i.e. nothing will happen). And also means that I can check if 
     //it was called and with as parameters (and many other things) 
     spyOn(Employees, 'GetEmployees'); 

     //This initializes the controller 
     var myController = $controller('EmployeeController', { 
      $scope: $rootScope.$new() 
      //You don't need to inject Employees here. It will be automatic 
      //injected for you. 
     }); 

     expect(Employees.GetEmployees).toHaveBeenCalled(); 
    })); 
}); 
相关问题