unit-testing - 将 html 与 testacularjs 集成时出错

标签 unit-testing continuous-integration jasmine fixtures

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

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

问题:我正在尝试使用 jasmine-jquery 进行此 DOM 测试,在我的 testacular.conf.js 中,我有允许将 html 文件加载到浏览器中的部分。
// 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 */** ];
但是,在命令行上的测试运行程序中,当我删除 html fixture 时(甚至在我编写任何 jasmine-jquery 代码来加载 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)
编辑 :问同样问题的另一种方式:我如何让 testacular 服务于 html 而不会炸毁测试运行器?

正如我上面提到的,我需要在配置中包含“test/fixtures/*.html”,但测试运行器会爆炸。

最佳答案

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

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

    python -m SimpleHTTPServer 3502 &

  • 将您的 fixture 文件放在适当的位置。我的是 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

  • 编辑 testacular.conf.js 以添加配置 block
    
    proxies = {
    '/fixtures' : 'http://localhost:3502/'
    };
    
  • 编辑您的 jasmine 单元测试,使其具有如下所示的 block
    
    beforeEach(function(){
            jasmine.getFixtures().fixturesPath = '/fixtures/test/fixtures';
        });
    

  • 现在您应该可以加载 fixture/读取 fixture 了

    关于unit-testing - 将 html 与 testacularjs 集成时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13019215/

    相关文章:

    javascript - SpyOn 一个 mock 的 Jest 模块没有正确监视

    java - Java中的自动内存泄漏检测

    c++ - 单元测试项目的物理布局是什么?

    ruby-on-rails - Rails 测试单元 - content_for 的助手

    ios - FaSTLane 构建并发布到 App Center 后应用程序无法启动

    qt - 将 gitLab-CI 用于 Qt 项目

    javascript - node.js 单元测试模拟依赖

    c++ - Sonar 无法读取我的 CPPUnit 报告

    javascript - Jasmine 监视继承的方法(使用 typescript )不能按预期使用 toHaveBeenCalled()

    jasmine - 使用 Karma、PhantomJS、Jasmine 测试滚动位置