簡體   English   中英

茉莉花單元測試

[英]Jasmine unit test

我正在使用Jasmine運行Angular單元測試。

我正在嘗試獲取routeParams id作為測試的一部分,但只是未定義。 我有什么想念的嗎?

謝謝

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

您無需在測試中初始化routeParams的ID。 相反,您正在創建另一個對象,並將此新對象傳遞給控制器​​:

// 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',
    },
    //...
});

如果您想將實際的$ routeParams服務傳遞給控制器​​,並且希望該服務具有ID,那么您所需要做的就是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM