mardi 13 septembre 2016

Difficulty in testing Angularjs controller with service as its dependency

I have written an angular test application.Trying to learn testing using it.

Controller looks like this

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);

and my angular service looks as this

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]);

I am trying to write unit tests for the controller,but not sure, how can i inject service as the dependency to the controller in unit testing.

Please help in this

Thanks

Aucun commentaire:

Enregistrer un commentaire