Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS:mock http服务(使用$httpBackend)用一些数据响应Promise API的成功方法_Angularjs_Unit Testing_Karma Jasmine_Angular Mock_Angular Promise - Fatal编程技术网

AngularJS:mock http服务(使用$httpBackend)用一些数据响应Promise API的成功方法

AngularJS:mock http服务(使用$httpBackend)用一些数据响应Promise API的成功方法,angularjs,unit-testing,karma-jasmine,angular-mock,angular-promise,Angularjs,Unit Testing,Karma Jasmine,Angular Mock,Angular Promise,我试图解释我问题的简单版本。 1) 我正在为控制器(myController)方法(减法)编写单元测试。 2) 使用httpbackend模拟http我想在subtract方法中返回http的success函数200的响应,以降低其在任何虚拟删除url的success函数中的值(在测试环境之外总是成功的)。 3) 但我看到expect(scope.testvalue)仅为5。 感谢您的帮助 'use strict'; describe('MyController UNIT TEST specs

我试图解释我问题的简单版本。
1) 我正在为控制器(myController)方法(减法)编写单元测试。
2) 使用httpbackend模拟http我想在subtract方法中返回http的success函数200的响应,以降低其在任何虚拟删除url的success函数中的值(在测试环境之外总是成功的)。
3) 但我看到expect(scope.testvalue)仅为5。 感谢您的帮助

'use strict';
describe('MyController UNIT TEST specs ', function () {    
var scope, http, location, ctrl, httpBackend;
beforeEach(module('myApp', 'ui.router')); 

beforeEach(inject(function ($rootScope, $http, $controller, $httpBackend) {
    scope = $rootScope.$new();
    http = $http;
    httpBackend = $httpBackend;
    ctrl = $controller('myController', { $scope: scope, $http: http});
    httpBackend.when('DELETE', 'url').respond(200, 'fsdf');
}));

afterEach(function () {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
});    

it('test 1 : Subtract 1 from a value enter code here` using subtract method in myController', function () {
    httpBackend.when('DELETE', 'url').respond(200);
    var testvalue = 5;
    scope.subtract(testvalue);
    expect(testvalue).toBe(4);
});
});
angular.module("myApp").controller("myController", function ($scope, $http) {    
    $scope.subtract = function (testValue) {    
        $http({
            method: 'DELETE',
            url: 'url'
        }).then(function (data) { //success    
            //irrespective of data subtract 1 here     
            testValue - 1 = 4;    
        }, function (errResult) { //fail
            console.log(errResult);
        });
    }
})
我看到的错误是(预期的)
错误:预期false为true。

在使用angular mock的单元测试中,您必须手动调用
httpBackend.flush()
以开始模拟http请求/响应,然后才能预期这样的结果:

it('test 1 : Subtract 1 from a value enter code here` using subtract method in myController', function () {
    httpBackend.when('DELETE', 'url').respond(200);
    var testObj = { testValue: 5 };
    scope.subtract(testObj);
    httpBackend.flush();
    expect(testObj.testValue).toBe(4);
});
$scope.subtract = function (testObj) {
    $http({
        method: 'DELETE',
        url: 'url'
    }).then(function (data) { //success
        //irrespective of data subtract 1 here
        testObj.testValue = testObj.testValue - 1;
    }, function (errResult) { //fail
        console.log(errResult);
    });
};
并且您的http回调不会修改
作用域.testvalue
,如果您希望
作用域
中的
testvalue
发生更改,则回调应如下所示:

it('test 1 : Subtract 1 from a value enter code here` using subtract method in myController', function () {
    httpBackend.when('DELETE', 'url').respond(200);
    var testObj = { testValue: 5 };
    scope.subtract(testObj);
    httpBackend.flush();
    expect(testObj.testValue).toBe(4);
});
$scope.subtract = function (testObj) {
    $http({
        method: 'DELETE',
        url: 'url'
    }).then(function (data) { //success
        //irrespective of data subtract 1 here
        testObj.testValue = testObj.testValue - 1;
    }, function (errResult) { //fail
        console.log(errResult);
    });
};

有关完整的工作示例,请参见:

感谢您的快速响应。很抱歉,我必须编辑代码。1) 错误似乎是错误:没有挂起的刷新请求!在httpBackend上调用刷新时。1) Sry它不是范围测试值,我是作为参数传递的,而是变量本身。您将httpBackend.flush()调用放在哪里了?在测试中的“scope.subtract(testvalue);”行之后,我已经更新了答案并包含了plunker示例。请看看这是不是你想要的。同样在javascript中,参数变量是按值传递的,所以如果你想在适当的位置修改变量,你必须,至少,将它包装在一个对象中,如答案所示。你能告诉我一件事吗。运行测试时是否进入成功功能?在我的例子中,它不是为了能够操作testObj而进入成功方法