2

我不确定 Reporters 是否缺少某些东西,但有没有一种简单的方法来执行我的单元测试并将结果作为字符串检索,而不是弄乱控制台或 DOM?

4

1 回答 1

7

为此,您必须实现自己的Reporter记录结果并将其保存为文本格式。这是一个简短的示例,说明如何做到这一点:

function TextReporter() {
    this.textResult = "";
}

TextReporter.prototype = new jasmine.Reporter();

TextReporter.prototype.onRunnerFinished = function (callback) {
    this.callbackEnd = callback;
};

TextReporter.prototype.reportRunnerResults = function (runner) {        
    // When all the spec are finished //
    var result = runner.results();

    this.textResult += "Test results :: (" + result.passedCount + "/" + result.totalCount + ") :: " + (result.passed() ? "passed" : "failed");
    this.textResult += "\r\n";

    if (this.callbackEnd) {
        this.callbackEnd(this.textResult);
    }
};

TextReporter.prototype.reportSuiteResults = function (suite) {
    // When a group of spec has finished running //
    var result = suite.results();
    var description = suite.description;
}

TextReporter.prototype.reportSpecResults = function(spec) {
    // When a single spec has finished running //
    var result = spec.results();

    this.textResult += "Spec :: " + spec.description + " :: " + (result.passed() ? "passed" : "failed");
    this.textResult += "\r\n";
};

之后HtmlReporter,您可以使用您的TextReporter.

var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;

var txtReporter = new TextReporter();
txtReporter.onRunnerFinished(function (text) {
    // Do something with text //
});

jasmineEnv.addReporter(txtReporter);

window.onload = function() {
    jasmineEnv.execute();
};

如果您需要有关自定义报告器的更多信息,您只需要知道它们必须实现Reporter接口。

于 2012-11-16T20:28:21.423 回答