如何对directive进行如何做单元测试试

I have an AngularJS directive that has a templateUrl defined.
I am trying to unit test it with Jasmine.
My Jasmine JavaScript looks like the following, per the recommendation of :
describe('module: my.module', function () {
beforeEach(module('my.module'));
describe('my-directive directive', function () {
var scope, $
beforeEach(inject(function (_$rootScope_, _$compile_, $injector) {
scope = _$rootScope_;
$compile = _$compile_;
$httpBackend = $injector.get('$httpBackend');
$httpBackend.whenGET('path/to/template.html').passThrough();
describe('test', function () {
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'
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
instead of .
If this is the culprit, how do I use the latter instead of the former?
Thanks in advance!
解决方案 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 /&');
本文地址: &
我有,有一个 templateUrl 定义的AngularJS指令。我试图单元茉莉花测试。我的茉莉花的JavaScript看起来像以下,每this: 描述('模块:my.module',函数(){
beforeEach(模块('my.module'));
描述(“我的指示性的指令”,函数(){
VAR范围,$编译;
beforeEach(注(功能(_ $ rootScope_,_ $ compile_,$喷油器){
范围= _ $ rootScope_;
$编译= _ $ compile_;
$ httpBackend = $ injector.get('$ httpBackend');
$ httpBackend.whenGET('路径/要/ template.html')直通()。
描述(“测试”,函数(){
beforeEach(函数(){
元素= $编译(
'<我的指导性和GT;< /我-指令>')(范围);
angular.element(document.body的).append(元);
afterEach(函数(){
element.remove();
它(“测试”,函数(){
期待(element.html())TOBE('自卫队')。
});}); 当我在茉莉规范的错误运行此我得到以下错误: 类型错误:对象#<对象&没有方法'直通' 我要的是要加载的templateUrl的是 - 我不希望使用响应。我相信这可以使用代替的。如果这是罪魁祸首,我怎么使用,而不是以前的后面?在此先感谢!解决方案 我最终什么事做得是越来越模板缓存,并把认为在那里。我没有不使用ngMock超调,事实证明:
beforeEach(注(功能(_ $ rootScope_,_ $ compile_,$ templateCache){
范围= _ $ rootScope_;
$编译= _ $ compile_;
$ templateCache.put('路径/要/ template.html','<模板的推移,这里/>');}));
本文地址: &
扫一扫关注官方微信学习AngularJS | RIA爱好者}

我要回帖

更多关于 如何编写单元测试 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信