В настоящее время я работаю над приложением, которое построено из Backbone.js, с Require.js для загрузки зависимостей. Я надеялся, что смогу загрузить QUnit и иметь страницу, на которой пользователь может просто загрузить страницу, и она будет динамически запускать юнит-тесты. Однако я попал в ловушку. Всякий раз, когда я загрузить страницу, тесты работают, но если я уйду со страницы, а затем вернуться, QUnit разрывы со следующей ошибкой: Uncaught Error: pushFailure() assertion outside test context,
Как использовать qunit в базовом приложении
Ниже приведен код, который я в настоящее время работаю. Мой реальный вопрос: с моим текущим макетом 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};
});
Возможный дубликат [Магистраль и требование добавления Qunit] (http://stackoverflow.com/questions/13991065/хребет-и-требует, как к надстройку QUnit) –