jasminekarma-jasminejasmine-nodejasmine-jquery

No more request expected in jasmine


Could anyone please tell me what I'm doing wrong here when I am trying to run this test case I get an error : Error: Unexpected request: GET "localhost:3000/home?extended=true" No more request expected how can I handle multiple HTTP and factory calls in one controller through jasmine? here is my test code

describe('homeController Controller', function(){
beforeEach(module('myapp'));
var ctrl,$http,$httpBackend,scope,trendingProducts,newArrivalsProducts,store,categories,HomeProduct,Product;
beforeEach(inject(function(Product,HomeProduct,$injector,_$q_,$controller, $rootScope, $httpBackend,$http) {
  var deferred = _$q_.defer();
Product = Product;
HomeProduct = HomeProduct;
$httpBackend = $injector.get('$httpBackend');
$http = $http;
trendingProducts = {};
newArrivalsProducts = {};
store = {};
categories = {};
trendingProducts = [];
rootScope = $rootScope;
scope = $rootScope.$new();
$httpBackend.when('GET','http://localhost:3000/home',{
'headers': {
  'Content-Type': 'application/json'
},
  'params': { extended: 'true' },
  'withCredentials':true
}).respond({'success':true});
deferred.resolve('resolveData');
spyOn(Product,'fetchProductList').and.returnValue(deferred.promise);
spyOn(HomeProduct,'fetchTrendingProduct').and.returnValue(deferred.promise);
spyOn(HomeProduct,'fetchNewArrivals').and.returnValue(deferred.promise);
spyOn(HomeProduct,'fetchStore').and.returnValue(deferred.promise);
spyOn(HomeProduct,'fetchCategories').and.returnValue(deferred.promise);
ctrl = $controller('homeController', {
  $scope: scope,
  trendingProducts:trendingProducts,
  newArrivalsProducts:newArrivalsProducts,
  store:store,
  categories:categories
});
 }));

 it('Check Controller Promise function"s of Home Controller', function() {
  rootScope.$apply(); 
 });
});

Solution

  • You've mocked your $httpBackend with 'http://localhost:3000/home' but you've called 'localhost:3000/home?extended=true' - with no 'http://'. Improve your regex. Try somthing like:

    $httpBackend.when('GET',/localhost:3000\/home/,...)
      .respond({'success':true});