首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用templateUrl对AngularJS指令进行单元测试

使用templateUrl对AngularJS指令进行单元测试
EN

Stack Overflow用户
提问于 2013-03-05 10:26:15
回答 7查看 70.4K关注 0票数 122

使用AngularJS。

有一个指令。

指令定义templateUrl

指令需要单元测试。

目前使用Jasmine进行单元测试。

This推荐的代码如下:

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'

templateUrl需要按原样加载

无法使用respond

可能与ngMock的使用有关,而与ngMockE2E的使用无关。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15214760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档