Javascript 无法模拟$httpbackend,因为Jasmine karma

Javascript 无法模拟$httpbackend,因为Jasmine karma,javascript,angularjs,unit-testing,karma-jasmine,httpbackend,Javascript,Angularjs,Unit Testing,Karma Jasmine,Httpbackend,我使用angular js,我有一个controller.js,我想为它编写jasmine测试用例。我能够在我的测试用例中调用controller的函数。我的控制器中有一个本地函数,由作用域中的方法调用(我正在编写测试用例),该方法使用ajax请求调用服务方法 我无法使用$httpbackend在测试用例中模拟此http post请求 (function () { 'use strict'; angular .module('coApp') .co

我使用angular js,我有一个controller.js,我想为它编写jasmine测试用例。我能够在我的测试用例中调用controller的函数。我的控制器中有一个本地函数,由作用域中的方法调用(我正在编写测试用例),该方法使用ajax请求调用服务方法

我无法使用$httpbackend在测试用例中模拟此http post请求

(function () {
    'use strict';
    angular
        .module('coApp')
        .controller('NewController', [
        '$http',
        '$q',
        '$log',
        '$modal',
        '$scope',
        'AgentDataLoader',
        '$timeout',
        NewController]);

    function NewController ($http, $q, $log, $modal, $scope,  $state, $rootScope, $filter, AgentDataLoader, $timeout) {

        //Some variables declarations

        //some methods 
        function myMethod($filter, a, b) {
            console.log("inside myMethod");
            if (angular.isDefined(a) && angular.isDefined(b)) {
                console.log("inside if ");
                console.log("a:: "+a);
                console.log("b:: "+b);

                dataLoader.callPOST(a, b)
                    .then(function (data) {
                    console.log("Data from service :: "+JSON.stringify(data));
                    /**calling some directive and methods 
                       I am unable to execute this since I am unable to mock http Post of above mentioned dataLoader
                    **/

                console.log("after http request");
            } else {
                //some other logic
            }
        }



        $scope.methodToBeTested= function (randomData) {
            console.log("selectedSystems called ****** ");
            //some logic
myMethod($filter, $scope.a, $scope.b);

        };
})();
我的茉莉花测试套件

describe('NewController',function(){
    var ctrl, scope, $httpBackend, $compile, parameters;
    beforeAll(function (done) {

    });    


    beforeEach(function () {
        module('MYmODULE');
        module('myServiceModule');
    });

    beforeEach(inject(function ($controller, _$httpBackend_, $rootScope, _$compile_) {
        scope=$rootScope.$new();
        $compile = _$compile_;
        $httpBackend = _$httpBackend_;
        ctrl=$controller('NewController',{$scope: scope});
        // Start listening to xhr requests

        //This data i want to send as a success of httpBackEnd Post
        success.data = JsonData;

        console.log('Setting request in $httpBackend');

        //Not working
        $httpBackend.whenPOST('/abc/efg').success(function(method, url, data) {
            console.log("Getting sample data from httpBackend :: "+url);
            return [200, success.data, {}];
        }); 

        //Not working
        $httpBackend.whenPOST('abc/efg').respond(function(method, url, data, headers){
            console.log('Received these data:', method, url, data, headers);
            phones.push(angular.fromJson(data));
            return [200, {}, {}];
        });

        $httpBackend.flush();    
    }));

    it("Testing myMethodToBeTested of the page", function(){ 
        scope.randomData=someRandomData;
        expect(scope.methodToBeTested(scope.randomData));

        //Tried this also not working
        $httpBackend.expectPOST('/abc/efg').respond(success.data);

        afterEach(function () {   
    });
});

对于您的测试用例,您必须模拟响应数据。我认为在上面的代码中,我没有看到success变量在任何地方被初始化。因此,如果您想要获得预期的响应,您必须在Success变量(基于上述发布的代码)中指定预期的对象,例如:

现在必须在POST/GET请求中传递此变量。像

$httpBackend.expectPOST('/abc/efg').respond(Success.data);

希望这有帮助:)

对于您的测试用例,您必须模拟响应数据。我认为在上面的代码中,我没有看到success变量在任何地方被初始化。因此,如果您想要获得预期的响应,您必须在Success变量(基于上述发布的代码)中指定预期的对象,例如:

现在必须在POST/GET请求中传递此变量。像

$httpBackend.expectPOST('/abc/efg').respond(Success.data);

希望这对您有所帮助:)

将您的状态代码更改为201,因为它是post call将您的状态代码更改为201,因为它是post call