将 html 与 testacularjs 集成时出错 [英] Error while integrating html with testacularjs

查看:11
本文介绍了将 html 与 testacularjs 集成时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 (html) 固定装置与 testacular 集成?是否有执行基于 DOM 的测试的建议或者它是一种反模式?

目标:我正在尝试测试一个自定义模块,该模块解析 DOM 树并创建一个新的数据结构.DOM 树可以是动态的(类似于 html/markdown 编辑器的内容),因此不适合进行端到端测试

问题:我正在尝试使用 jasmine-jquery 进行此 DOM 测试,在我的 testacular.conf.js 中,我有允许将 html 文件加载到浏览器中的部分.

<上一页><代码>//要在浏览器中加载的文件/模式列表文件 = [茉莉花,茉莉花适配器,'test/spec/**/*.js','test/fixtures/*.html' **/* 需要包含在此处才能提供 */**];

但是,在我的命令行测试运行程序中,当我删除 html 夹具时(甚至在我编写任何 jasmine-jquery 代码来加载夹具之前),我收到以下错误消息:

<上一页><代码>Chrome 22.0 **错误**Uncaught SyntaxError: Unexpected token <在/Users/myUser/myProject/test/fixtures/fixture_template.html:1 Chrome 22.0:执行 0 of 0 错误(0.143 秒/0 秒)

编辑:问同样问题的另一种方式:如何让 testacular 为 html 提供服务而不破坏测试运行器?

正如我上面提到的,我需要在配置中包含test/fixtures/*.html",但测试运行器会崩溃.

解决方案

当前版本的 testacularjs 不支持这个.但是,testacularjs(Vojta Jina) 的作者建议我使用代理解决方案通过不同的 Web 服务器提供 html 来解决此问题.对于那些好奇的人,这里是完成这项工作的端到端步骤.

  • 首先通过运行如下命令来运行网络服务器

    <块引用>

    python -m SimpleHTTPServer 3502 &

  • 将您的夹具文件放在适当的位置.我的是 test/fixtures/first.html

    <块引用>

    现在您应该可以访问 [http://localhost:3502/test/fixtures/first.html] 并在检查页面源代码时看到标记

  • 编辑 testacular.conf.js 以添加配置块

    <代码>代理 = {'/fixtures' : 'http://localhost:3502/'};

  • 编辑您的 jasmine 单元测试,使其具有如下所示的块

    <代码>之前(函数(){jasmine.getFixtures().fixturesPath = '/fixtures/test/fixtures';});

现在您应该可以加载夹具/读取夹具了

How do I integrate (html) fixtures with testacular? Is there any recommendation for performing DOM based tests or is it an anti-pattern?

Objective : I am trying to test a custom module which parses the DOM tree and creates a new data structure. The DOM tree can be dynamic (like contents of a html/markdown editor) and hence is not a good candidate for end to end testing

Problem : I am trying to use jasmine-jquery for this DOM testing and in my testacular.conf.js, I have the section to allow loading of html files into the browser.


// list of files / patterns to load in the browser
files = [
  JASMINE,
  JASMINE_ADAPTER,
  'test/spec/**/*.js',
  'test/fixtures/*.html' **/* Needs to be included here to be served */**
];

However in my test runner on the command line, I get the following error message when I drop the html fixture(even before I write any jasmine-jquery code to load the fixture):


Chrome 22.0 **ERROR**
    Uncaught SyntaxError: Unexpected token < at /Users/myUser/myProject/test/fixtures/fixture_template.html:1 Chrome 22.0: Executed 0 of 0 ERROR (0.143 secs / 0 secs)

EDIT : Another way to ask the same question : How do I get testacular to serve html and not blow up the test runner?

As I mentioned above, I need to include the 'test/fixtures/*.html" in the config but the test runner just blows up.

解决方案

Current version of testacularjs cannot support this. However, the author of testacularjs(Vojta Jina), suggested I use a proxy solution to workaround this by serving the html through a different web server. For those curious, here are the end to end steps to get this working.

  • First run the webserver by running a command like the following

    python -m SimpleHTTPServer 3502 &

  • Drop your fixture file(s) in appropriate location. Mine was test/fixtures/first.html

    Now you should be able to visit [http://localhost:3502/test/fixtures/first.html] and see the markup when you inspect page source

  • Edit testacular.conf.js to add the config block

    
    proxies = {
    '/fixtures' : 'http://localhost:3502/'
    };
    

  • Edit your jasmine unit test to have a block like the following

    
    beforeEach(function(){
            jasmine.getFixtures().fixturesPath = '/fixtures/test/fixtures';
        });
    

Now you should be in a position to loadfixture/readfixture

这篇关于将 html 与 testacularjs 集成时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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