Require.JS和JS测试驱动程序: [英] Require.JS and JS Test Driver: Unexpected token <

查看:242
本文介绍了Require.JS和JS测试驱动程序:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试通过RequireJS加载的简单的Backbone模型:

I am trying to test a simple Backbone Model loaded via RequireJS:

define ["backbone"], (Backbone)->

    class Todo extends Backbone.Model 
        defaults:
            title: ''
            priority: 0
            done: false

        validate: (attrs) -> 
            errs = {}
            hasErrors = false

            if (attrs.title is "")
                hasErrors = true
                errs.title = "Please specify a todo"

            if hasErrors
                return errs

        toggleDone: ->
            @save("done", !@get("done"))

    return Todo

我的测试看起来像:

requirejs.config 
    baseUrl: "js/"
    paths: 
        jquery: "https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min"
        jqueryui: "https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min"
        json2: "http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2"
        underscore: "http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min"
        backbone: "http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min"
        backboneLocalStorage: "https://raw.github.com/jeromegn/Backbone.localStorage/master/backbone.localStorage-min"
    shim: 
        "underscore":
            exports: "_"
        "backbone": 
            deps: ["jquery", "underscore", "json2"]
            exports: "Backbone"
        "jqueryui": 
            deps: ["jquery"]
        "backboneLocalStorage":
            deps: ["backbone"]
            exports: "Backbone.LocalStorage"

require ["models/todo"], (Todo) ->

    console.log Todo

    TodoTests = TestCase("TodoTests")

    TodoTests::testCreateTodo = -> 
        todo = new Todo({ title: "Hello" })
        assertEquals "Hello", todo.get("title")
        assertEquals 0, todo.get("priority")
        assertEquals false, todo.get("done")

JS测试驱动程序配置: / p>

The JS Test Driver config:

server: http://localhost:3001

load:
  - ../public/js/libs/require.js
  - ../public/js/tests.js

serve:
  - ../public/js/models/*
  - ../public/js/collections/*
  - ../public/js/views/*

从Chrome控制台的JS测试驱动程序侦听页面上看到的问题:

Problem seen from the JS Test Driver listened page on Chrome console:


未捕获的语法错误:意外的令牌<

Uncaught SyntaxError: Unexpected token <

查看来自Chrome的Todo.js,

Looking at Todo.js from Chrome,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Console Runner</title><script type="text/javascript">var start = new Date().getTime();</script>
Uncaught SyntaxError: Unexpected token <
<script src="/static/lib/json2.js" type="text/javascript"></script><script src="/static/lib/json_sans_eval.js" type="text/javascript"></script><script src="/static/jstestdrivernamespace.js" type="text/javascript"></script><script src="/static/lib/jquery-min.js" type="text/javascript"></script><script src="/static/runner.js" type="text/javascript"></script><script type="text/javascript">jstestdriver.runConfig = {'debug':false};</script>
<script type="text/javascript">jstestdriver.console = new jstestdriver.Console();

注意它的HTML页面而不是我的实际JS。此外,由于返回HTML页面代替JS,因此 console.log(Todo)返回 undefined

Notice its a HTML page instead of my actual JS. Also console.log(Todo) returns undefined since a HTML page is returned in place of a JS. Did I configure this wrongly?

推荐答案

我一直在努力工作几天,在Google上无休止地搜索,直到我终于找到了JsTestDriver做。在您的 requirejs.config 中,为了测试正常运行,您的 baseUrl 需要为:

I struggled with this for days and searched with Google endlessly until I finally found out what JsTestDriver was doing. In your requirejs.config, for your tests to run properly, your baseUrl needs to be:

baseUrl: /test/path/to/your/stuff

原因是当JsTestDriver创建它的服务器时,它将 / test / 添加到所有目录。如果 baseUrl 设置不正确,它会开始尝试向本地发送对窗口的引用。

The reason is when JsTestDriver makes its server, it prepends /test/ to all the directories. If the baseUrl isn't set properly, it starts trying to send back local a reference to window I think.

我看到的唯一的其他问题可能会遇到运行测试与作为第一行的require语句。我有Jasmine,并把我的测试的顶部的require()使他们从来不运行,所以我必须在我的beforeEach为我的测试,并获得对象,然后他们运行。

The only other problem that I see you may run into is running the tests with the require statement as the first line. I have Jasmine and putting my require() at the top of my tests caused them to never run so I had to do it in my beforeEach for my tests and get the object before they ran.

我可能会做错事,虽然我看到无数其他人声称Jasmine的require语句工作,所以我的狩猎继续。

I'm probably doing something wrong though I see countless other people claiming that the require statement in Jasmine works, so my hunt continues.

这篇关于Require.JS和JS测试驱动程序:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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