Javascript Can';t使用$httpBackend测试POST$http请求
试图通过单元测试覆盖我的应用程序的http请求。我使用Jasmine和Karma,并使用本教程逐步编写代码:$httpBackend。但我有一个错误:Javascript Can';t使用$httpBackend测试POST$http请求,javascript,angularjs,jasmine,karma-runner,karma-jasmine,Javascript,Angularjs,Jasmine,Karma Runner,Karma Jasmine,试图通过单元测试覆盖我的应用程序的http请求。我使用Jasmine和Karma,并使用本教程逐步编写代码:$httpBackend。但我有一个错误: Error: No pending request to flush ! at Function.$httpBackend.flush (eval at <anonymous> (app/index.js:105:1), <anonymous>:1839:41) at Object.eval
Error: No pending request to flush !
at Function.$httpBackend.flush (eval at <anonymous> (app/index.js:105:1), <anonymous>:1839:41)
at Object.eval (eval at <anonymous> (app/index.js:137:1), <anonymous>:37:20)
来自控制器的代码块:
$scope.status;
$scope.isFetching = false;
const handleResponse = response => {
$scope.status = response.status;
$scope.isFetching = false;
};
$scope.next = (email, password) => {
$scope.isFetching = true;
$http
.post('http://localhost:5000/register', { email, password })
.then(response => handleResponse(response))
.catch(response => handleResponse(response));
};
UPD
我还尝试在每个$httpBackend.flush()
之前添加以下内容(如下所示):
但无论如何,没有任何变化。在此处创建控制器后调用$httpBackend.flush()
it('should successfully register user', () => {
const controller = createController();
$httpBackend.flush(); //<-- this is not necessary if controller creation doesn't involve $http calls
$httpBackend.expectPOST('http://loalhost:5000/register').respond();
$scope.next('hello@gmail.com', '123456');
$httpBackend.flush();
});
哦,我的上帝。我花了大约两个小时来解决这个问题。非常感谢你!
if(!$rootScope.$$phase) {
$rootScope.$apply();
}
it('should successfully register user', () => {
const controller = createController();
$httpBackend.flush(); //<-- this is not necessary if controller creation doesn't involve $http calls
$httpBackend.expectPOST('http://loalhost:5000/register').respond();
$scope.next('hello@gmail.com', '123456');
$httpBackend.flush();
});
it('should successfully register user', () => {
const controller = createController();
$httpBackend.expectPOST('http://loalhost:5000/register').respond();
$scope.next('hello@gmail.com', '123456');
$httpBackend.flush();
});