Require.JS和JS Test Driver:意外的令牌<

时间:2012-06-07 09:47:30

标签: coffeescript requirejs js-test-driver

我正在尝试测试通过RequireJS加载的简单Backbone模型:

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 Test Driver配置:

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 Test Driver侦听页面看到的问题:

  

未捕获的SyntaxError:意外的标记<

从Chrome看看Todo.js,

<!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。此外,console.log(Todo)返回undefined,因为返回的HTML页面代替JS。我错误地配置了吗?

2 个答案:

答案 0 :(得分:3)

我挣扎了几天,不停地搜索谷歌,直到我终于找到了JsTestDriver正在做的事情。在requirejs.config中,要使测试正常运行,您的baseUrl必须是:

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

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

我看到你遇到的唯一另一个问题是运行带有require语句作为第一行的测试。我有Jasmine并且将我的require()放在测试的顶部导致它们永远不会运行所以我必须在我的beforeEach中进行测试并在它们运行之前获取对象。

我可能做错了,虽然我看到无数其他人声称Jasmine中的require语句有效,所以我的追捕仍在继续。

答案 1 :(得分:1)

你检查了你的ajax回复吗?我只是有同样的事情而且'&lt;'是来自资源404'd ...

时返回的开放doctype标记