Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 指令中的单元测试$routeParams_Angularjs_Unit Testing_Angularjs Directive_Jasmine_Angular Mock - Fatal编程技术网

Angularjs 指令中的单元测试$routeParams

Angularjs 指令中的单元测试$routeParams,angularjs,unit-testing,angularjs-directive,jasmine,angular-mock,Angularjs,Unit Testing,Angularjs Directive,Jasmine,Angular Mock,我有一个指令可以访问页面的$routeParams: myApp.directive("myList", function ($routeParams) { return { restrict: 'E', templateUrl: 'tabs/my-list.html', link: function (scope) { scope.year = $routeParams.year; } };

我有一个指令可以访问页面的
$routeParams

myApp.directive("myList", function ($routeParams) {
    return {
        restrict: 'E',
        templateUrl: 'tabs/my-list.html',
        link: function (scope) {
            scope.year = $routeParams.year;
       }
    };
});
该指令按预期工作,并正确访问
$routeParams

我正在尝试使用angular mock/jasmine进行测试。我不知道如何将mock
$routeParams
传递给指令。这就是我所拥有的:

describe('myList', function () {
    var scope, compile, element, compiledDirective;
    var mockParams = { 'year': 1996 };

    beforeEach(function () {
        module('templates', 'MyApp');

        inject(function ($compile, $rootScope, $routeParams) {
            compile = $compile;
            scope = $rootScope.$new();
        });
        element = angular.element('<my-list></my-list>');
        compiledDirective = compile(element)(scope);
        scope.$digest();
    });
    it('should fill in the year', function () {
        expect(scope.year).toEqual(mockParams.year);
    });
});
description('myList',函数(){
变量范围,编译,元素,编译方向;
var mockParams={'year':1996};
beforeach(函数(){
模块(“模板”、“MyApp”);
注入(函数($compile、$rootScope、$routeParams){
compile=$compile;
scope=$rootScope.$new();
});
元素=角度。元素(“”);
compiledDirective=编译(元素)(范围);
范围。$digest();
});
它('应填写年份',函数(){
预期(范围年)、toEqual(模拟参数年);
});
});

这显然不起作用,因为我从未将
mockParams
传递给指令。有办法吗?

使用
角度模拟
$routeParams
对象
mockParams
。扩展
直接将
mockParams
对象分配给
$routeParams
。这样,在编译指令之前,
$routeParams
将可用

inject(function ($compile, $rootScope, $routeParams) {
    compile = $compile;
    scope = $rootScope.$new();
    angular.extend($routeParams, mockParams);
});

只使用
$routeParams.year=1996如何在创建元素之前?