2016-05-13 86 views
1

我有类似下面问题茉莉嘲讽服务

(function() { 

    var mockController = function ($scope, MockService) { 
     $scope.message = "This is a text message"; 
     $scope.getCities = function() { 
      return MockService.getCities(); 
     }; 

    }; 


    var mockService = function ($http) { 
     this.getCities = function() { 
      return $http.get("../rest/url", { 
       headers: { 
        'Accept': 'application/yang.data+json' 
       } 
      }); 
     }; 
    }; 

    angular.module("MockApp", []) 
     .service("MockService", mockService) 
     .controller("MockController", mockController); 

}()) 

我想写一个UT嘲讽服务像下面

describe("MockController", function() { 

    var $scope; 

    beforeEach(function() { 
     module("MockApp"); 
     inject(function (_$controller_, _$rootScope_, MockService) { 
      $scope = _$rootScope_.$new(); 
      spyOn(MockService, "getCities").and.callFake(function() { 
       return [{ 
        city: "Bangalore" 
        , country: "India" 
       }]; 
      }); 
      controller = _$controller_("MockController", { 
       $scope: $scope 
      }); 

     }); 
    }); 

    describe("Test", function() { 

     it("Should be Bangalore", function() { 
      $scope.getCities() 
       .then(function (data) { 
        console.log("got it"); 
       }) 
     }); 
    }); 

}); 

它抛出一个错误说

控制器时

TypeError:$ scope.getCities(...)。然后不是函数

请帮帮我。

+0

这可能是一个愚蠢的问题,而是如何为角的$ HTTP服务如何注入到你的服务?此外,您已为您的MockService.getCitites()创建了一个不返回承诺对象的假。不要以为你可以在伪返回类型上调用.then()函数。 – TSmith

+0

它存在于angular.js中 – robin

回答

0

我想:

$scope.getCities = function() { 
    return MockService.getCities(); 
}; 

应该是:

$scope.getCities = function() { 
    return MockService(getCities()); 
};