angularjs:在范围内测试服务调用。$ on

时间:2015-01-22 07:06:09

标签: angularjs angularjs-directive jasmine

我有一个指令如下:

app.directive('Directive', ['MyService', '$state',
    function (MyService, $state) {
        return {
            restrict: 'E',
            scope: {},
            replace: true,
            templateUrl: 'partials/sample.html',
            link: function (scope, element, attrs) {

                scope.$on('onEvent', function (event, result) {
                    MyService.getInfo($state.params.param1, result.data.value1, result.data.value2)
                        .then(function (response) {
                            scope.varX = response.data;
                        });
                });
            }
        };
    }]);

为该指令编写的测试用例如下:

it('MyDirective test case', function () {
    mockBackend.whenGET('partials/sample.html').respond('<div>Test Template</div>');
    var scope = rootScope.$new(),
        element,
        expectedResult = [
            {
                "data": "dummy"
            }];

    element = compile('<MyDirective></MyDirective>')(scope);
    scope.$digest();
    expect(scope.$broadcast).toHaveBeenCalledWith('onEvent', [{value1: 'val1', val2: 'val2'}]);
    mockBackend.flush();

    var testScope = element.isolateScope();
    expect(testScope.varX).toEqual(expectedResult);
});

在运行测试用例时,我收到以下错误:

Directive: appDirectives MyDirective test case FAILED
Expected undefined to equal [ { data : 'dummy' } ].

testScope没有分配var变量。 我在测试用例中做错了什么?

0 个答案:

没有答案