2

レポーターに欠けているものがあるかどうかはわかりませんが、コンソールや 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 に答える