How to unit test angularjs with asp.net webservice back end

I have a web application written in javascript using Angularjs and I am trying to write unit tests using jasmine 2.1.3.

However, the back end of the application is written in ASP.NET 4.5 and I am using ASP WebService to make client calls call my api.

  • How can I handle these web service calls when writing unit tests for controllers in jasmine?
  • Is there a method similar to ngMock and $ httpbackend for ASP.Net Webservice?

Sample code:

Jasmine:

describe('ActivityController', function() {
beforeEach(module('app'));

var $controller;

beforeEach(inject(function(_$controller_){
  $controller = _$controller_;
}));

describe('Get Employees', function() {

var $scope, controller;

beforeEach(function() {
  $scope = {};
  controller = $controller('Activity', { $scope: $scope });
});

it('returns list of employees', function() {
  $scope.loadEmps();
  expect($scope.emps.length).not.toEqual(0);
});
});
});

      

controller

angular.module('app').controller('Activity', function ($scope, SharedService, $state, toastr, ngTableParams, $filter) {
//get employees
$scope.loadEmps = function () {
    WebService.getUsers(var1, var2, var3, var4, function (UsersList) {
        // Add Users
        if (UsersList.users != null && UsersList.users.length > 0) {
            $scope.emps = UsersList.users;
            userFriendly = UsersList.users[0].friendlyname;
        }
        else {
            $scope.emps = [];
        }
        $scope.$apply();
    }, function () {
        toastr.error("Could Not Get Users");
        $scope.emps = [];
        $scope.$apply();
    });
 }
 });

      

Web service:

[WebMethod]
public group getUsers(string var1, int var2, string var3, string var4)
{
    group response = apiFactory.getGroup(var1, var2, var3);
    response = apiFactory.SortGroup(response, var4);
    return response;
}

      

+3


source to share


1 answer


You need to write unit tests for JavaScript and the backend, and then write integration tests for how the two work together. How to do this is not a stack overflow question; it will match better on Programmers or Quality Assurance sites.



+1


source







All Articles