【问题标题】:How to use qunit in a backbone app如何在主干应用程序中使用 qunit
【发布时间】:2014-02-07 15:22:22
【问题描述】:

我目前正在开发一个由 Backbone.js 构建的应用程序,使用 Require.js 来加载依赖项。我曾希望能够加载 QUnit 并拥有一个页面,用户可以在其中简单地加载该页面,并且它将动态运行单元测试。然而,我遇到了一个障碍。每当我加载页面时,测试都会工作,但如果我离开页面然后返回,QUnit 会因以下错误而中断: Uncaught Error: pushFailure() assertion outside test context,

以下是我目前正在使用的代码。我真正的问题是:在我目前的 Backbone.js 布局中,是否可以按照我想要的方式使用 QUnit,还是应该放弃这个功能?

页面加载时,渲染父视图service_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};
});

【问题讨论】:

标签: javascript jquery backbone.js requirejs qunit


【解决方案1】:

Qunit.start() 不会开始测试。 start() 用于异步测试,如果您将其注释掉,您的测试可能会继续运行,尽管我不知道 require.js

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    相关资源
    最近更新 更多