Jasmine 使用 templateUrl 测试 AngularJS 指令 [英] Jasmine tests AngularJS Directives with templateUrl

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

问题描述

我正在用 Jasmine 为 AngularJS 编写指令测试,并在其中使用 templateUrl:https://gist.github.com/tanepiper/62bd10125e8408def5cc

I'm writing directive tests for AngularJS with Jasmine, and using templateUrl with them: https://gist.github.com/tanepiper/62bd10125e8408def5cc

但是,当我运行测试时,我得到了包含在要点中的错误:

However, when I run the test I get the error included in the gist:

Error: Unexpected request: GET views/currency-select.html

从我在文档中读到的内容,我认为我这样做是正确的,但似乎并非如此 - 我在这里遗漏了什么?

From what I've read in the docs I thought I was doing this correctly, but it doesn't seem so - what am I missing here?

谢谢

推荐答案

如果您使用的是 ngMockE2E 或 ngMock:

所有 HTTP 请求都使用您指定的规则在本地处理, 被传递到服务器.由于模板是通过 HTTP 请求的,因此它们也在本地进行处理.由于当您的应用尝试连接到 views/currency-select.html 时,您没有指定要执行的任何操作,因此它会告诉您它不知道如何处理它.您可以轻松地告诉 ngMockE2E 传递您的模板请求:

If you're using ngMockE2E or ngMock:

all HTTP requests are processed locally using rules you specify and none are passed to the server. Since templates are requested via HTTP, they too are processed locally. Since you did not specify anything to do when your app tries to connect to views/currency-select.html, it tells you it doesn't know how to handle it. You can easily tell ngMockE2E to pass along your template request:

$httpBackend.whenGET('views/currency-select.html').passThrough();

请记住,如果您愿意,也可以在路由路径中使用正则表达式来传递所有模板.

Remember that you can also use regular expressions in your routing paths to pass through all templates if you'd like.

文档对此进行了更详细的讨论:http://docs.angularjs.org/api/ngMockE2E.$httpBackend

The docs discuss this in more detail: http://docs.angularjs.org/api/ngMockE2E.$httpBackend

您需要使用 $injector 来访问新的后端.来自链接的文档:

You'll need to use the $injector to access the new backend. From the linked docs:

var $httpBackend;
beforeEach(inject(function($injector) {
  $httpBackend = $injector.get('$httpBackend');
  $httpBackend.whenGET('views/currency-select.html').respond(200, '');
}));

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

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