P
P
Peter2016-06-07 17:47:09
Angular
Peter, 2016-06-07 17:47:09

How to test logic with data that comes from the server in angular?

I need to test the logic with the data that comes from the server in the controller. But I don't understand how to properly write tests with mocha+chai.
My code:

angular
  .module('WebApp', [])
  .factory('api', function($q, $http){
    return function(type, url, data){
    var deferred  = $q.defer(),
      url       = 'ConfigBaseUrl' + url; //'ConfigBaseUrl' меняется таской gulp-а из конфигурационного файла.
        
    switch (type){
      case 'GET':
      $http.get(url)
        .success(function (data) {
        deferred.resolve(data);
        })
        .error(function (data) {
        deferred.reject(data);
        });
        break;
      case 'PUT':
      $http.put(url, data)
        .success(function (data) {
        deferred.resolve(data);
        })
        .error(function (data) {
        deferred.reject(data);
        });
        break;
      case 'POST':
      $http.post(url, data)
        .success(function (data) {
        deferred.resolve(data);
        })
        .error(function (data) {
        deferred.reject(data);
        });
        break;
      case 'DELETE':
      $http.delete(url, data)
        .success(function (data) {
        deferred.resolve(data);
        })
        .error(function (data) {
        deferred.reject(data);
        });
        break;
      default:
        console.log('');
        return;
    }

    return deferred.promise;
    }
  })
  .service('serverApi', function(api){
      this.getList = function (url) {
      return api('GET', url);
    };
  })
  .constant('URL', {
    LIST:           '/api/list'
  })
  .controller('listCtrl', function($location, serverApi){
    var pathLocation = $location.path(),
    
    pathLocation = pathLocation.split('/');
    
    serverApi.getList(URL.LIST+'/'+pathLocation[2]).then(successDataTable, errorDataTable);
    
    function successDataTable(response){
      //some logic
    }
    
    function errorDataTable(response){
      //some logic
    }
  });

And the test code:
describe('CONTROLLER: listCtrl', function() {
      var scope        = null,
        vm           = null,
      $q           = null,
      deferred     = null,
      $httpBackend = null;
      
    beforeEach(module('WebApp'));

    beforeEach(inject(function($rootScope, $controller, _$q_, _$httpBackend_) {
    $q          = _$q_;
    scope       = $rootScope.$new();
    deferred    = _$q_.defer();
    $httpBackend = _$httpBackend_;
    vm          = $controller('gameCtrl', {$scope: scope});
    }));

    it('exist controller', function(){
    expect(vm).to.exist;
    });
    
    it('send json data in successDataTable', function(){
      //Что мне тут нужно написать?
    });
  })

There are examples for jasmin on the internet, but I use mocha+chai.
Please tell me how to correctly pass data in json format for successDataTable in the test.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-06-07
Protko @Fesor

1) we push $http into services
2) we mock these services
3) we either test the services working with the network within e2e tests or through $httpBackendMock. But it makes little sense to cover them with unit tests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question