【问题标题】:Resolve CSV Stringify promise within a test or end a test when a function is called?在测试中解决 CSV Stringify 承诺或在调用函数时结束测试?
【发布时间】:2018-07-06 02:16:42
【问题描述】:

我正在尝试使用 AngularJS 模拟 CSV Stringify...但我只想获取传入的两个参数并在稍后的测试中使用它们来测试某些选项是否正确。

it("converts latitude and longitude correctly", function() {
  this.CSV._stringifyPromise._setResolveMode("instant");
  var csvFeatures, csvOptions;
  this.CSV.stringify.and.callFake(function(csvFeaturesInStringify,
    csvOptionsInStringify) {
    csvFeatures = csvFeaturesInStringify;
    csvOptions = csvOptionsInStringify;
  });

  this.featureExportCSVService.buildCsvFile(this.features, "Test");
  expect(this.csvOptions[0].LONG).toEqual(this.features[0].geometry.coordinates[
    0].toString());
  expect(this.csvOptions[0].LAT).toEqual(this.features[0].geometry.coordinates[
    1].toString());
});

我相信 CSV stringify 在非测试代码中返回一个承诺,因为它被称为......

    CSV.stringify(csvObject.features, csvObject.options)
      .then(function(result) { ... }

我以为它只会在我的假电话中返回结果,但这似乎不起作用。

  this.CSV.stringify.and.callFake(function(csvFeaturesInStringify,
    csvOptionsInStringify) {
    expect(...);
    expect(...);
    return "test,blah,blah";
  });

我尝试使用 $q.defer().resolve();,但 $q 未定义。做一个回报它只是说undefined is not a constructor

我将如何解决此函数以仅返回一个虚拟字符串,甚至只是在此处结束测试并评估我的 expect() 调用?

【问题讨论】:

    标签: angularjs jasmine karma-jasmine


    【解决方案1】:

    需要像这样使用 then 函数和 successCallback 调用返回......

      this.CSV.stringify.and.callFake(function(csvFeaturesInStringify, csvOptionsInStringify) {
        csvFeatures = csvFeaturesInStringify;
        return {
          then: function(successCallback) {
            successCallback("geometry,LAT,LONG,name,marker-color");
          }
        };
    

    然后在最终到达该方法的函数调用之后,您需要将期望块放在那里而不是在这里。

      this.featureExportCSVService.buildCsvFile(this.features, "Test");
      expect(this.csvOptions[0].LONG).toEqual(this.features[0].geometry.coordinates[
        0].toString());
      expect(this.csvOptions[0].LAT).toEqual(this.features[0].geometry.coordinates[
        1].toString());
    

    【讨论】:

      猜你喜欢
      • 2016-12-18
      • 2020-10-21
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多