How can I create jasmine tests for a Rectangular scenario like the one shown below?
script
$scope.allEmployees = [{
visible: true,
empId: "EMP148"
}];
$scope.employeeDocuments = { "EMP148": [{
"empId": "EMP148",
"department": "Sales",
"firstName": "Manu",
"lastName": "Mathew",
"place": "Kolkata"
}]
};
var employeesCopy = angular.copy($scope.allEmployees);
$scope.allEmployees.push({visible: true, empId: "EMP489"});
$scope.addEmployees = function (employeesCopy) {
var newEmployee = {visible: true, empId: "EMP489"};
var emplyeeList = {
employees: $scope.allEmployees,
newEmployee: newEmployee
};
Restangular.all('emp/getEmployees').post(emplyeeList).then(function (employees) {
if (!_.isEmpty(employees[emplyeeList.newEmployee.empId])) {
if ($scope.employeeDocuments.hasOwnProperty(emplyeeList.newEmployee.empId)) {
delete $scope.employeeDocuments[emplyeeList.newEmployee.empId];
}
$scope.employeeDocuments[emplyeeList.newEmployee.empId] = employees[emplyeeList.newEmployee.empId];
setMyEmployees(true);
$scope.flag = true;
console.log("success");
} else {
$scope.employeeDocuments = employeesCopy;
console.log("no documents");
}
$scope.flag = false;
}, function (error) {
console.log("failed");
$scope.flag = false;
});
};
$scope.addEmployees(employeesCopy);
setMyEmployees = function (flag)
{
// other implementation
};
I have written test cases as shown below, but I am encountering an exception stating
spyOn could not find an object to spy upon for all()
test case
WORKING DEMO - contains the full logic and test cases
describe('Testing Controllers', function() {
describe('Testing EmployeeController Controller', function() {
var EmployeeController, $scope, $httpBackend, Restangular;
beforeEach(function() {
module('emp');
});
beforeEach(inject(function($controller, $rootScope, $filter, $injector, Restangular, $httpBackend) {
$scope = $rootScope.$new();
$httpBackend = $httpBackend;
Restangular = $injector.get("Restangular");
EmployeeController = $controller('EmployeeController ', {
$rootScope: $rootScope,
$scope: $scope,
$filter: $filter
});
}));
it('should add new employees when addEmployees() is called', inject(function($httpBackend)
{
$scope.allEmployees = [{
visible: true,
empId: "EMP148"
}];
$scope.employeeDocuments = { "EMP148": [{
"empId": "EMP148",
"department": "Sales",
"firstName": "Manu",
"lastName": "Mathew",
"place": "Kolkata"
}]
};
var employeesCopy = angular.copy($scope.allEmployees);
spyOn(Restangular, 'all').andCallThrough();
var newEmployee = {visible: true, empId: "EMP489"};
var emplyeeList = {
employees: $scope.allEmployees,
newEmployee: newEmployee
};
var mockToReturn = {
"EMP489": [{
"empId": "EMP489",
"department": "Sales",
"firstName": "Ram",
"lastName": "Mohan",
"place": "Delhi"
}]
};
$scope.addEmployees(employeesCopy);
$httpBackend.expectPOST('emp/getEmployees',emplyeeList).respond(mockToReturn);
expect(Restangular.all).toHaveBeenCalledWith('emp/getEmployees');
expect(setMyEmployees(true)).toHaveBeenCalled();
}));
});
});