mercredi 27 janvier 2016

Unable to test .success function within controller

I have a simple controller and service that performs a HTTP POST. The controller has .success and .error callbacks for which i am trying to write some unit tests:

To call btnClick:

<button ng-click="btnClick('cart')">Click</button>

Controller:

app.controller('MyController', function($scope, myService, $timeout) {

    $scope.btnClick = function (action) {

        $scope.postData = {"ID": $scope.myId, "user": $scope.myUser};

        $scope.cartRequest = function() {
            $scope.disabled = true;
            $scope.myText = '';

            myService.postAction('cart', $scope.postData)
                .success(function (data, status, headers, config) {
                    $timeout(function(){
                        $scope.disabled = false;
                        $scope.myText = 'Inside Timeout';

                        $timeout(function(){
                            $scope.hideBtn = true;
                        }, 1400);

                    }, 1500);                    
                })                    
                .error(function (data, status, headers, config) {
                   $scope.cartError();
                });
        };

        switch(action) {
            case "cart":
                $scope.cartRequest();
                break;
        }
    };

MyService:

 app.factory('MyService', function ($http) {

    return {
        postAction: function(uri, postData) {
            return $http({
                method: 'POST',
                url: '/cartInfo/' + uri,
                data: postData,
                headers: {'Content-Type': 'application/json'}
            });
        }
    };
});
});

My Test:

describe('Cart API: can get cart details', function () {
    var myService, httpBackend;

    var customerInfo = {
            "accountId" : "12345678901",
            "userName" : "MyTestUser",
            "firstName" : "Joe",
            "lastName" : "Bloggs"
    };

    beforeEach(inject(function($rootScope, $controller) {
        scope = $rootScope.$new();        

        ctrl = $controller('MyController', {$scope:scope});

        spyOn(scope, 'btnClick').and.callThrough();

        inject(function ($httpBackend, _MyService_, $timeout) {
            MyService = _MyService_;
            httpBackend = $httpBackend;
            timeout = $timeout;
        });
    }));

    it('should check if a user is already logged in', function () {    
        scope.customer = customerInfo;
        scope.btnClick('cart');

        var returnData = {};
        var result = {};
        var mockData = { "accountId" : scope.customer.accountId, "userName" : scope.customer.userName};

        httpBackend.expectPOST('/cartInfo/cart', mockData, function () {
            return {
                'Content-Type': 'application/json'
            };
        }).respond(200, returnData);        



        myService.postAction('unlock', mockData).success(function (response) {
            console.log("hello");
            timeout.flush(1501);
            scope.$apply();
            expect(scope.disabled).toBeFalsy();
            expect(scope.myText).toEqual('Inside Timeout');
        });

    });
});

It seems as though the .success block within my test is never called as the 2 expects:

expect(scope.disabled).toBeFalsy();
expect(scope.myText).toEqual('Inside Timeout');

Dont seem to run.

Aucun commentaire:

Enregistrer un commentaire