问题描述
我在写指令测试茉莉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请求时,也将在本地处理。既然你没有指定什么做当你的应用程序试图连接到的意见/货币select.html
,它告诉你它不知道如何处理它。您可以轻松地告诉ngMockE2E沿着你的模板要求通过:
When you use 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();
请记住,您还可以使用常规的前pressions在你的路由路径通过所有的模板,如果你愿意的话。
Remember that you can also use regular expressions in your routing paths to pass through all templates if you'd like.
该文档详细讨论这一点:<一href=\"http://docs.angularjs.org/api/ngMockE2E.$httpBackend\">http://docs.angularjs.org/api/ngMockE2E.$httpBackend
The docs discuss this in more detail: http://docs.angularjs.org/api/ngMockE2E.$httpBackend
更新
仅供参考,你需要使用 $注射器
来访问新的后端。从链接的文档:
FYI, 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, '');
}));
这篇关于茉莉花测试使用templateUrl AngularJS指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!