Pianoc
Pianoc

Reputation: 807

Jasmine unit test

I am using Jasmine to run an Angular unit test.

I am trying to get the routeParams id as part of my test but just get undefined. Is there something I am missing?

Thanks

beforeEach(inject(function($controller, $rootScope,_$routeParams_, _$q_){
    scope = $rootScope.$new();
    rootScope = $rootScope;
    rootScope.go = jasmine.createSpy();
    routeParams = _$routeParams_;
    $q = _$q_;

    deliveryId = '7666';
    eanScanned = '5012663020351';
    confirmedList = _.findWhere(data, {'id': deliveryId});

    controller = TestHelper.createController('GoodsReceiptConfirmController', {
        $rootScope: rootScope,
        $scope: scope,
        $routeParams : {
            'id': '7666',
        },
        GoodsReceiptService : GoodsReceiptService

    });
}));


describe('on confirmation page open', function(){

    it('if has routeParams. display said delivery and intReceipts', function(){
        console.log(routeParams.id);
        //expect(scope.routeParams.id).toBeDefined();
        //GoodsReceiptService.getDelivery();


        //expect(scope._initReceipts).toHaveBeenCalled();
        //expect(scope._initReceipts).toHaveBeenCalledWith(scope.delivery);
    });
});

Upvotes: 0

Views: 109

Answers (1)

JB Nizet
JB Nizet

Reputation: 692211

You're never initializing the id of the routeParams in the test. Instead, you're creating another object and passing this new object to the controller:

// this stores the actual, angular-created service into the routeParams variable
routeParams = _$routeParams_;

// this passes a "fake" object literal containing an id to the controller function.
controller = TestHelper.createController('GoodsReceiptConfirmController', {
    $routeParams : {
        'id': '7666',
    },
    //...
});

If you want to pass the actual $routeParams service to the controller, and want this service to have an ID, all you need is

routeParams = _$routeParams_;
routeParams.id = '7666';
controller = TestHelper.createController('GoodsReceiptConfirmController', {
    $rootScope: rootScope,
    $scope: scope,
    GoodsReceiptService : GoodsReceiptService
});

Upvotes: 1

Related Questions