单元测试AngularJS指令与templateUrl

单元测试AngularJS指令与templateUrl

我有一个templateUrl定义的AngularJS指令。我正在尝试用Jasmine进行单元测试。

根据以下建议,我的Jasmine JavaScript如下所示:

describe('module: my.module', function () {
    beforeEach(module('my.module'));

    describe('my-directive directive', function () {
        var scope, $compile;
        beforeEach(inject(function (_$rootScope_, _$compile_, $injector) {
            scope = _$rootScope_;
            $compile = _$compile_;
            $httpBackend = $injector.get('$httpBackend');
            $httpBackend.whenGET('path/to/template.html').passThrough();
        }));

        describe('test', function () {
            var element;
            beforeEach(function () {
                element = $compile(
                    '<my-directive></my-directive>')(scope);
                angular.element(document.body).append(element);
            });

            afterEach(function () {
                element.remove();
            });

            it('test', function () {
                expect(element.html()).toBe('asdf');
            });

        });
    });});

当我在我的Jasmine规范错误中运行它时,我收到以下错误:

TypeError: Object #<Object> has no method 'passThrough'

我想要的是模板加载原型 - 我不想使用respond。我相信这可能与使用ngMock而不是ngMockE2E有关。如果这是罪魁祸首,我该如何使用后者而不是前者呢?

提前致谢!


慕妹3242003
浏览 515回答 3
3回答

慕慕森

我最终做的是获取模板缓存并将视图放在那里。事实证明,我无法控制不使用ngMock:beforeEach(inject(function(_$rootScope_,&nbsp;_$compile_,&nbsp;$templateCache)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$scope&nbsp;=&nbsp;_$rootScope_; &nbsp;&nbsp;&nbsp;&nbsp;$compile&nbsp;=&nbsp;_$compile_; &nbsp;&nbsp;&nbsp;&nbsp;$templateCache.put('path/to/template.html',&nbsp;'<div>Here&nbsp;goes&nbsp;the&nbsp;template</div>');}));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

AngularJS