Javascript 如何在主干应用程序中使用qunit

Javascript 如何在主干应用程序中使用qunit,javascript,jquery,backbone.js,requirejs,qunit,Javascript,Jquery,Backbone.js,Requirejs,Qunit,我目前正在开发一个基于Backbone.js构建的应用程序,使用Require.js加载依赖项。我曾希望能够加载QUnit,并拥有一个页面,用户只需加载该页面,它就会动态运行单元测试。然而,我遇到了一个障碍。每当我加载页面时,测试都会工作,但如果我离开页面,然后返回,QUnit会出现以下错误: Uncaught错误:测试上下文之外的pushFailure()断言, 下面是我目前正在使用的代码。我真正的问题是:在我当前的Backbone.js布局中,以我想要的方式使用QUnit是可行的,还是应该放

我目前正在开发一个基于Backbone.js构建的应用程序,使用Require.js加载依赖项。我曾希望能够加载QUnit,并拥有一个页面,用户只需加载该页面,它就会动态运行单元测试。然而,我遇到了一个障碍。每当我加载页面时,测试都会工作,但如果我离开页面,然后返回,QUnit会出现以下错误:
Uncaught错误:测试上下文之外的pushFailure()断言,

下面是我目前正在使用的代码。我真正的问题是:在我当前的Backbone.js布局中,以我想要的方式使用QUnit是可行的,还是应该放弃这个特性

加载页面时,将呈现父视图
service\u test.view.js

define(['backbone', 'hgn', 'statemachine_test_view'],
function (Backbone, hgn, StateMachineTest) {
    var DynamicTest = Backbone.View.extend({
        // This is the main element of the application, it is what is cleared and populated for each page
        el: $(".overwatch_container"),
        // Build the Statemachine test view (statemachine_test.view.js)
        statemachine_view: new StateMachineTest(),

        render: function (data) {
            // Empty out anything that's in the container already
            this.$el.empty();

            // Contain the 'this' reference, so it can be used throughout
            var that = this;

            // Pull in and populate the hogan template for the main parent elements
            require(['hgn!templates/service_test.template.hogan'], function (tmpl) {
                // JSON object with all of thd page's information to pass to the templates
                resultset = {
                            "service_type": data.service_type
                };

                // Render the template with the given information, and then build child views
                if( that.$el.html( tmpl.render( resultset ) ) )
                {
                    // Build the child view
                    that.statemachine_view.render();
                }
            });

        },

        close: function () {
            $(this.$el).empty();
            return this;
        }

    });

    // Return the view object, so it can be utilized when this script is require'd
    return DynamicTest;
});
statemachine_test.view.js是创建StateMachineTest的位置:

define(['backbone', 'hgn'],
function (Backbone, hgn) {
    var StateMachineTest = Backbone.View.extend({

        render: function (options) {
            // Dynamically set the element associated with this view, as it is not instantiated when this is first included by Require.js
            this.setElement( $(".test_content") );

            // Contain the 'this' reference, so it can be used throughout
            var that = this;

            require(['hgn!templates/qunit_base.template.hogan'], function (tmpl) {
                // JSON object with all of thd page's information to pass to the templates

                // Render the template with the given information, and then build child views
                if( that.$el.html(tmpl.render()) )
                {

                    // Once the template has been rendered, load the qUnit test script
                    // 'statemachine_dynamic' = statemachine_dynamic.test.js
                    require(['QUnit', 'statemachine_dynamic'], function(QUnit, statemachine) {
                        statemachine.run();

                        QUnit.load();
                        QUnit.start(); //THIS IS THE LINE THE ERROR POINTS TO

                        QUnit.done( function (details) {
                            _.each($("#qunit-tests li").children("a"), function (child) {
                                $(child).attr("href", function (index, attr) {
                                    return attr+window.location.hash;
                                });
                            });

                        });

                    });
                }
            });

        },

    });

    return StateMachineTest;
});
这是我实际的测试脚本statemachine_dynamic.test.js:

define([], function () {
    var run = function() {
        module("Statemachine Testing");
        test( "Test 1", function() {
            var value = "hello";
            equal( value, "hello", "We expect value to be hello" );
        });
        test( "Test 2", function() {
            var value = "hello";
            equal( value, "hello", "We expect value to be hello" );
        });
        test( "Test 3", function() {
            var value = "hello";
            equal( value, "hello", "We expect value to be hello" );
        });
    };
    return {run: run};
});
quonit.start()
不会启动测试
start()
用于异步测试,如果您注释掉它,您的测试可能也会运行,尽管我不知道require.js是否可能重复