测试指令使用模板 [英] Testing directives that uses templates
问题描述
如何使用templateUrl我的单元测试指令来加载模板?
How can i unit test directives that use templateUrl to load templates?
由于$ httpBackend是一个模拟,它也不会加载模板。
我希望能够使用像
Since $httpBackend is a mock, it will not load templates either. I would like to be able to use something like
$httpBackend.whenGET(/^\/views\//).passThrough();
和让它实际上得到了模板,但我还没有想出如何正确地做到这一点。
and let it actually get the templates, but I haven't figured out how to do this correctly.
我觉得我有一些困惑关于如何单元测试指令。
免责声明:我没有经验的测试,或使用茉莉也testacular
I think I have something confused regarding how to unit test directives. Disclaimer: I have no experience testing, or using jasmine nor testacular.
任何帮助是AP preciated。
Any help is appreciated.
推荐答案
感谢pkozlowski.opensource领导我朝着正确的方向!
Thanks to pkozlowski.opensource for leading me in the right direction!
对于任何人想知道如何解决它:
For anyone wondering how I solved it:
- 添加 https://npmjs.org/package/grunt-angular-templates 您的项目。
- 添加咕噜构建任务,所有的模板编译成一个js文件。
这JS文件现在注册一个模块(名称可以在gruntfile配置)。
This JS file will now register a module (name can be configured in the gruntfile).
在所有的测试依赖于模板,你必须加载这个模块。
In all your tests dependant on templates you have to load the this module.
试验例:
'use strict';
describe('Component: comments', function() {
beforeEach(module('studentportalenApp'), module('app.templates'));
var element;
it('should render an error message if type is not recognized', inject(function($rootScope, $compile) {
element = angular.element('<comments></comments>');
element = $compile(element)($rootScope);
expect(element.html()).toBe('Comments directive type not recognized.');
}));
});
要小心使用作为app.templates模块中定义完全相同的网址,让您的看法。即/视图/,而不是观点/,否则不会与反正请求的模板缓存路径和火相匹配。
Be careful to get your views using the exact same url as defined in the app.templates module. I.e. /views/ and not views/, or else it will not match up with the template cache paths and fire of the request anyways.
这篇关于测试指令使用模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!