无法模仿$ httpbackend whenPOST为Jasmine-karma

时间:2017-02-17 03:15:46

标签: javascript angularjs unit-testing karma-jasmine httpbackend

我正在使用角度js,我有一个controller.js我想写茉莉花测试用例。我可以在我的测试用例中调用控制器的功能。我的控制器中有一个本地函数,它由范围内的方法调用(我正在编写测试用例),它正在调用带有ajax请求的服务方法。

我无法使用$ httpbackend

在我的测试用例中模拟此http发布请求
(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);

        };
})();

我的Jasmine测试套件

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 () {   
    });
});

1 个答案:

答案 0 :(得分:0)

对于Testcase Suit,您必须模拟响应数据。我认为在上面的代码中,我没有看到成功变量在任何地方被初始化。因此,如果您想获得预期的响应,您必须在Success变量(基于上面发布的代码)中分配预期对象,例如Ex:

var Success = {data:{SomeKey:"value"}}

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

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

希望这会有所帮助:)