JS如何使用Teaspoon测试(文档).ready函数

时间:2018-08-14 11:07:47

标签: javascript sinon teaspoon

我正在尝试为我拥有的JS文件构建测试用例,我正在使用Teaspoon和Sinon。但是我有麻烦去纠正它。从Teaspoon中看不到功能,我也不知道如何进行测试。 JS在ready函数中有2个Ajax函数,我需要测试调用。在JS文件及其测试下面。

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();
            }
          }
        }
      });
    }

});

测试JS:

//= require rspec_helper
//= require ad_hoc_report_results
//= require sinon

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

var resultsLoaded = false;
var resultsTable;

describe("Ad Hoc Report Results", function() {
  var reportId = 123;

  var run_results = {
    "columns":[["Form Identifier"],["Form Version"],["Question Text"],["Mapping"],["Submission"],["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() {
    html_1 =
      '  <table id="results" class="table table-striped table-bordered table-condensed dataTable no-footer">' +
      '    <thead>' +
      '      <tr>' +
      '        <th>Form Identifier</th>' +
      '            <th>Form Version</th>' +
      '            <th>Question Text</th>' +
      '        <th>Mapping</th>' +
      '        <th>Submission</th>' +
      '         <th>C Code</th>'+
      '        </tr>' +
      '      </thead>' +
      '      <tbody>' +
      '    </tbody>' +
      '  </table>';
    fixture.set(html_1);

    /*html_2 =
      '  <table id="results" class="table table-striped table-bordered table-condensed dataTable no-footer">' +
      '    <thead>' +
      '      <tr>' +
      '        <th>URI</th>' +
      '            <th>Identifier</th>' +
      '            <th>Label</th>' +
      '        </tr>' +
      '      </thead>' +
      '      <tbody>' +
      '    </tbody>' +
      '  </table>';
    fixture.set(html_2);*/

    server = sinon.fakeServer.create();

  });

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

  it("Gets results", function () {

    resultsLoaded = false;

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

    server.respondWith("GET", "/ad_hoc_reports/"+ reportId +"/run_results", [
      200, {"Content-Type": "application/json"}, 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("CDASH DEMO");

  });

  it("Gets Status", function () {
    server.respondWith("GET", "/ad_hoc_reports/" + reportId + "/run_progress", [
      200, {"Content-Type":"application/json"}, JSON.stringify(run_results)
    ]);
  });

  it("checking", function() {
    server.respond();
    //getResults();
    server.respond();
    server.restore();
  });

});

0 个答案:

没有答案