JS测试如何测试错误警报

时间:2018-08-16 12:02:24

标签: javascript sinon teaspoon

我正在测试JS脚本,当出现问题时,其中一个功能显示错误。我想测试错误警报是否显示,但我不知道如何正确执行。

我的功能是:

function getStatus() {
    $.ajax({
      url: "/ad_hoc_reports/" + reportId + "/run_progress",
      type: "GET",
      dataType: 'json',
      error: function (xhr, status, error) {
        displayError("An error has occurred obtaining the run status.");
      },
      success: function(result){
        if (!result.running) {
          getResults();
          spRemoveSpinner("#running");
        } else {
          statusTimeout();
          // Just load the table once to set up page nicely.
          if (!resultsLoaded) {
            getResults();
          }
        }
      }
    });
  }

我想测试的是,错误部分的显示是伪造的。我在尝试以下内容:

it("Get status, display error", function () {
    stub_get_result = sinon.stub(window, 'getResults');
    stub_get_status = sinon.stub(window, 'getStatus');
    stub_error = sinon.stub(window, "displayError");

    var server_status = sinon.fakeServer.create();

    var run_status = {running: false};
    server_status.respondWith("GET", "/ad_hoc_reports/123/run_progress", [
      200, {"Content-Type": "application/json"},
      JSON.stringify(run_status)
    ]);

    server.respond();

    expect(stub_error.calledOnce).to.be.false;
    server.restore();
  });

我对JS和测试后的完整代码:

$(document).ready(function() {

  var resultsLoaded = false;
  var reportId = document.getElementById("report_id").value;
  var resultsTable;

  spAddSpinner("#running");
  getStatus();

  function statusTimeout() {
    setTimeout(function(){
      getStatus();
    }, 10000);
  }

  function getResults() {
    if (resultsLoaded) {
      resultsTable.ajax.reload();
    } else {
      resultsTable = $('#results').DataTable( {
        "ajax": {
          "url": "/ad_hoc_reports/" + reportId + "/run_results",
          "dataSrc": "data"
        },
        "pageLength": pageLength,
        "lengthMenu": pageSettings
      });
      resultsLoaded = true;
    }
  }

  function getStatus() {
    $.ajax({
      url: "/ad_hoc_reports/" + reportId + "/run_progress",
      type: "GET",
      dataType: 'json',
      error: function (xhr, status, error) {
        displayError("An error has occurred obtaining the run status.");
      },
      success: function(result){
        if (!result.running) {
          getResults();
          spRemoveSpinner("#running");
        } else {
          statusTimeout();
          // Just load the table once to set up page nicely.
          if (!resultsLoaded) {
            getResults();
          }
        }
      }
    });
  }

}); 

//= require rspec_helper
//= require ad_hoc_report_results
//= require spinner
//= require sinon
//= require application

describe("Ad Hoc Report Results", function () {

  var reportId = 123;
  var clock;

  var pageSet = [[5, 10, 15, 25, 50, 100, -1], ["5", "10", "15", "25", "50", "100", "All"]];
  var pageLen = 10;

  var run_results = {
    "columns": [{"data": "Form Identifier"}, {"data": "Form Version"}, {"data": "Question Text"}, {"data": "Mapping"}, {"data": "Submission"}, {"data": "C Code"}],
    "data": [["CDASH DEMO", "0.0.1", "Units", "HEIGHT_VSORRESU", "in", "C48500"],
      ["CDASH DEMO", "0.0.1", "Pulse Unit", "PULSE_VSORRESU", "beats/min", "C49673"],
      ["CDASH DEMO", "0.0.1", "Blood Pressure Unit", "BP_SYSBP_ORRESU", "mmHg", "C49670"],
      ["CDASH DEMO", "0.0.1", "Blood Pressure Unit", "BP_DIABP_VSORRESU", "mmHg", "C49670"]]
  };

  beforeEach(function () {
    clock = sinon.useFakeTimers();
    var resultsTable;
    $resultsLoaded = false;
    window.pageSettings = [[5, 10, 15, 25, 50, 100, -1], ["5", "10", "15", "25", "50", "100", "All"]];
    window.pageLength = 10;

    html = '' +
      '<div id="report_id">123</div> ' +
      '<button id="running" type="button" class="btn btn-default"></button> ' +
      '<table id="results" class="table table-striped table-bordered table-condensed dataTable no-footer" role="grid" ' +
      '       aria-describedby="results_info"> ' +
      ' <thead>' +
      '   <tr role="row">' +
      '     <th class="sorting_asc" tabindex="0" aria-controls="results" rowspan="1" colspan="1" ' +
      '         style="width: 173px;" aria-sort="ascending" ' +
      '         aria-label="Domain Prefix: activate to sort column descending">Form Identifier</th>' +
      '     <th class="sorting" tabindex="0" aria-controls="results" rowspan="1" colspan="1"' +
      '         style="width: 171px;" aria-label="Domain Label: activate to sort column ascending">Form Version</th>' +
      '     <th class="sorting" tabindex="0" aria-controls="results" rowspan="1" colspan="1" style="width: 107px;" ' +
      '         aria-label="Variable: activate to sort column ascending">Question Text</th>' +
      '     <th class="sorting" tabindex="0" aria-controls="results" rowspan="1" colspan="1" ' +
      '         style="width: 261px;" aria-label="Code List Short Name: activate to sort column ascending">Mapping</th>' +
      '     <th class="sorting" tabindex="0" aria-controls="results" rowspan="1" colspan="1" style="width: 213px;" ' +
      '         aria-label="Code List C Code: activate to sort column ascending">Submission</th>' +
      '     <th class="sorting" tabindex="0" aria-controls="results" rowspan="1" colspan="1" style="width: 213px;" ' +
      '         aria-label="Code List C Code: activate to sort column ascending">C Code</th>' +
      '   </tr> ' +
      ' </thead>' +
      ' <tbody> ' +
      '   <tr class="odd">' +
      '     <td colspan="5" class="dataTables_empty" valign="top">Loading...</td>' +
      '   </tr>' +
      ' </tbody>' +
      '</table>';
    fixture.set(html);

    server = sinon.fakeServer.create();

  });

  afterEach(function () {
    clock.restore();
    server.restore();
  });

  it("Gets results", function () {

    stub_get_result = sinon.stub(window, 'getResults');
    stub_status_timeout = sinon.stub(window, 'statusTimeout');

    $resultsLoaded = true;
    //var run_status = {running: false};

    //var server_status = sinon.fakeServer.create();

    server.respondWith("GET", "/ad_hoc_reports/123/run_results", [
      200, {"Content-Type": "application/json"},
      '[{"Form Identifier":"CDASH DEMO", "Form Version":"0.0.1", "Question Text":"Units", "Mapping":"HEIGHT_VSORRESU", "Submission":"in", "C Code": "C48500"}]',
      JSON.stringify(run_results)
    ]);

    expect($("#results thead th:nth-child(1)").text()).to.eq("Form Identifier");
    expect($("#results thead th:nth-child(2)").text()).to.eq("Form Version");
    expect($("#results thead th:nth-child(3)").text()).to.eq("Question Text");
    expect($("#results thead th:nth-child(4)").text()).to.eq("Mapping");
    expect($("#results thead th:nth-child(5)").text()).to.eq("Submission");
    expect($("#results thead th:nth-child(6)").text()).to.eq("C Code");

    expect($("#results tbody tr td:nth-child(1)").text()).to.eq("Loading...");

    stub_get_result(reportId);
    server.respond();
    expect($resultsLoaded).to.eql(true);
    server.restore();

  });

  it("DataTable Been Call", function() {
    $.prototype.DataTable = sinon.stub();

    $resultsLoaded = false;

    window.pageSettings = pageSet;
    window.pageLength = pageLen;

    stub_get_result(reportId);

    expect($.prototype.DataTable.calledOnce).to.be.false;

  });

  it("Gets Status result loaded true", function () {
    $resultsLoaded = true;
    var run_status = {running: false};
    stub_get_result = sinon.stub(window, 'getResults');
    stub_status_timeout = sinon.stub(window, 'statusTimeout');
    stub_spAddSpinner = sinon.stub(window, 'spAddSpinner');
    stub_spRemoveSpinner = sinon.stub(window, 'spRemoveSpinner');

    var server_status = sinon.fakeServer.create();

    server_status.respondWith("GET", "/ad_hoc_reports/123/run_progress", [
      200, {"Content-Type": "application/json"},
      JSON.stringify(run_status)
    ]);

    server_status.respond();
    server_status.restore();

    stub_get_result(reportId);
    stub_status_timeout;
    stub_spAddSpinner;
    stub_spRemoveSpinner;
    server_status.respond();

    expect(stub_get_result.calledOnce).to.be.true;
    expect(stub_spAddSpinner.calledOnce).to.be.false;
    expect(stub_spRemoveSpinner.calledOnce).to.be.false;
    expect(stub_status_timeout.calledOnce).to.be.false;
  });

  it("Gets Status result loaded false", function () {
    $resultsLoaded = false;
    var run_status = {running: true};
    stub_get_result = sinon.stub(window, 'getResults');
    stub_status_timeout = sinon.stub(window, 'statusTimeout');
    //stub_spAddSpinner = sinon.stub(window, 'spAddSpinner');
    //stub_spRemoveSpinner = sinon.stub(window, 'spRemoveSpinner');

    var server_status = sinon.fakeServer.create();

    server_status.respondWith("GET", "/ad_hoc_reports/123/run_results", [
      200, {"Content-Type": "application/json"},
      JSON.stringify(run_status)
    ]);

    server_status.respond();

    stub_get_result(reportId);
    stub_status_timeout;
    stub_spAddSpinner;
    stub_spRemoveSpinner;

    expect(stub_get_result.calledOnce).to.be.true;
    expect(stub_spAddSpinner.calledOnce).to.be.false;
    expect(stub_spRemoveSpinner.calledOnce).to.be.false;
    expect(stub_status_timeout.calledOnce).to.be.false;

    server_status.restore();
  });

  it("Get status, display error", function () {
    stub_get_result = sinon.stub(window, 'getResults');
    stub_get_status = sinon.stub(window, 'getStatus');
    stub_error = sinon.stub(window, "displayError");

    var server_status = sinon.fakeServer.create();

    var run_status = {running: false};
    server_status.respondWith("GET", "/ad_hoc_reports/123/run_progress", [
      200, {"Content-Type": "application/json"},
      JSON.stringify(run_status)
    ]);

    server.respond();

    expect(stub_error.calledOnce).to.be.false;
    server.restore();
  });

  it("Gets Timeout", function () {
    stub_status_timeout = sinon.stub(window, 'statusTimeout');
    var logSpy = sinon.spy(stub_status_timeout);
    stub_status_timeout;
    expect(logSpy).to.not.have.been.called;
    clock.tick(10010);
    expect(logSpy).to.have.been.calledOnce;
  });

});

0 个答案:

没有答案