我正在使用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);
});
});
答案 0 :(得分:1)
您永远不会在测试中初始化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
});