单元测试AngularJS指令与templateUrl [英] Unit Testing AngularJS directive with templateUrl

查看:333
本文介绍了单元测试AngularJS指令与templateUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,有一个 templateUrl 定义的AngularJS指令。我试图单元茉莉花测试。

I have an AngularJS directive that has a templateUrl defined. I am trying to unit test it with Jasmine.

我的茉莉花的JavaScript看起来像以下,每<一个推荐href=\"http://stackoverflow.com/questions/14761045/jasmine-tests-angularjs-directives-with-templateurl\">this:

My Jasmine JavaScript looks like the following, per the recommendation of 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');
            });

        });
    });
});

当我在茉莉规范的错误运行此我得到以下错误:

When I run this in my Jasmine spec error I get the following error:

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

我要的是要加载的templateUrl的是 - 我不希望使用响应。我相信这可以使用 ngMock 代替的ngMockE2E 。如果这是罪魁祸首,我怎么使用,而不是以前的后面?

All I want is for the templateUrl to be loaded as is - I don't want to use respond. I believe this may be related to it using ngMock instead of ngMockE2E. If this is the culprit, how do I use the latter instead of the former?

在此先感谢!

推荐答案

我最终什么事做得是越来越模板缓存,并把认为在那里。我没有不使用ngMock超调,事实证明:

What I ended up doing was getting the template cache and putting the view in there. I don't have control over not using ngMock, it turns out:

beforeEach(inject(function (_$rootScope_, _$compile_, $templateCache) {
    scope = _$rootScope_;
    $compile = _$compile_;
    $templateCache.put('path/to/template.html', '.<template-goes-here />');
}));

这篇关于单元测试AngularJS指令与templateUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆