Answer the question
In order to leave comments, you need to log in
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
}
});
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(){
//Что мне тут нужно написать?
});
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question