Hi, i try to testing my SearchService that pull my API.
I'm using Karma with Jasmine and mocking API with $httpBackend, but
Restangular don't send GET request and $httpBackend don't intercept these,
then *resolvedVal* is empty.
If using $http the $httpBackend and test case working correctly.
This is my code
Thank you!
// Service
(function(){
'use strict'
/** @ngInject */
function SearchService(Restangular){
var vm = this;
vm.getJournalists = getJournalists;
config();
function config(){
vm.restangularSearch =
Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('http://localhost:3000/api');
});
}
function getJournalists(){
if(typeof vm.journalists === 'undefined'){
return vm.restangularSearch
.all('journalists')
.getList();
}
return vm.journalists;
}
}
angular
.module('fooModule')
.service('SearchService', SearchService);
})()
(function(){
describe('Search', function(){
beforeEach(module('restangular'));
beforeEach(module('fooModule'));
var SearchService;
var $httpBackend;
var Restangular;
var journalists = [
{
"name": "J1",
"id": 220203,
"ruolo": "Scrittore"
},
{
"name": "J2",
"id": 22303,
"ruolo": "Scrittore"
},
{
"name": "J3",
"id": 22203,
"ruolo": "Scrittore"
}];
var outlets = [
{
"name": "T1",
"id": 220203,
"location": "Roma"
},
{
"name": "T2",
"id": 211103,
"location": "Roma"
},
{
"name": "T3",
"id": 200011103,
"location": "Roma"
}];
beforeEach(inject(function(_SearchService_, $injector){
$httpBackend = $injector.get("$httpBackend");
SearchService = _SearchService_;
Restangular = $injector.get("Restangular");
http = $injector.get("$http");
rootScope = $injector.get("$rootScope");
$httpBackend.whenGET('http://localhost:3000/api/journalists')
.respond(journalists)
$httpBackend.whenGET('http://localhost:3000/api/outlets')
.respond(outlets);
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('Restangular to be defined', function(){
expect(Restangular).toBeDefined();
});
it('service to be defined', function(){
expect(SearchService).toBeDefined();
});
it('service retrieve journalists', function(){
var resolvedVal;
var promise = SearchService.getJournalists();
promise.then(function(result){
console.log(result)
*resolvedVal* = result;
});
$httpBackend.flush();
expect(resolvedVal).toBeDefined();
});
})
})()
// These are the files imported through Karma
[ { pattern: 'bower_components/jquery/dist/jquery.js' },
{ pattern: 'bower_components/angular/angular.js' },
{ pattern: 'bower_components/angular-cookies/angular-cookies.js' },
{ pattern: 'bower_components/angular-sanitize/angular-sanitize.js' },
{ pattern: 'bower_components/angular-messages/angular-messages.js' },
{ pattern: 'bower_components/angular-resource/angular-resource.js' },
{ pattern:
'bower_components/angular-ui-router/release/angular-ui-router.js' },
{ pattern: 'bower_components/angular-bootstrap/ui-bootstrap-tpls.js' },
{ pattern: 'bower_components/malarkey/dist/malarkey.min.js' },
{ pattern: 'bower_components/angular-toastr/dist/angular-toastr.tpls.js'
},
{ pattern: 'bower_components/moment/moment.js' },
{ pattern: 'bower_components/lodash/lodash.js' },
{ pattern: 'bower_components/restangular/dist/restangular.js' },
{ pattern:
'bower_components/angu-fixed-header-table/angu-fixed-header-table.js' },
{ pattern: 'bower_components/underscore/underscore.js' },
{ pattern:
'bower_components/angular-underscore-module/angular-underscore-module.js' },
{ pattern: 'bower_components/angular-mocks/angular-mocks.js' },
{ pattern: 'src/app/**/*.module.js' },
{ pattern: 'src/app/**/*.js' },
{ pattern: 'src/**/*.spec.js' },
{ pattern: 'src/**/*.mock.js' },
{ pattern: 'src/**/*.html' },
{ pattern: 'src/assets/**/*',
included: false,
served: true,
watched: false } ]
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.