我正在尝试使用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字符串化会在非测试代码中返回一个承诺,因为它称为...
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()
调用?
答案 0 :(得分:0)
需要使用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());