2014-03-12 148 views
0

我正在使用Karma在角度指令中测试函数。该函数执行正常,但其中的http.get请求似乎被忽略,其他人抛出unexpected get错误。Karma忽略角度http获取请求

beforeEach(inject(function($injector, $compile, $rootScope){ 
    $gCompile = $compile; 
    $gScope = $rootScope; 
    $httpBackend = $injector.get('$httpBackend'); 
    $gHttp = $httpBackend; 
})); 

it("Should login to the UI", function() { 

    //Compile directive 
    var element = angular.element('<my-app></my-app>'); 
    $gCompile(element)($gScope); 

    //Reference it's local scope 
    var dirScope = element.scope(); 

    $gScope.$digest(); 
    dirScope.login(); 
}); 

获取请求在登录功能。

回答

0

如果您可以告诉我们login()函数是什么样子,可能会有所帮助,但现在看起来您没有正确使用$httpBackend

看看http://docs.angularjs.org/api/ngMock/service/$httpBackend

您应该对$httpBackend设置期望值,然后致电login()函数,然后在$httpBackend上调用flush()

+0

谢谢,调用flush看起来已经做到了。 – user3310447