Skip to content

Commit 40612fa

Browse files
spy部分测试
1 parent fc1df01 commit 40612fa

File tree

5 files changed

+173
-159
lines changed

5 files changed

+173
-159
lines changed

test/common/iServer/SurfaceAnalystServiceSpec.js

Lines changed: 124 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
require('../../../src/common/iServer/SurfaceAnalystService');
2+
require('../../../src/common/util/FetchRequest');
3+
//require('../../resources/SurfaceAnalystServiceResult');
24

35
var surfaceAnalystEventArgsSystem = null,
46
serviceFailedEventArgsSystem = null;
5-
7+
//var spatialAnalystURL = GlobeParameter.spatialAnalystURL;
8+
var spatialAnalystURL = "http://supermap:8090/iserver/services/spatialanalyst-sample/restjsr/spatialanalyst";
69
function surfaceAnalystCompleted(surfaceAnalystEventArgs) {
710
surfaceAnalystEventArgsSystem = surfaceAnalystEventArgs;
811
}
9-
1012
function surfaceAnalystFailed(serviceFailedEventArgs) {
1113
serviceFailedEventArgsSystem = serviceFailedEventArgs;
1214
}
13-
14-
var spatialAnalystURL = GlobeParameter.spatialAnalystURL;
15-
1615
function initSurfaceService() {
1716
return new SuperMap.SurfaceAnalystService(spatialAnalystURL, {
1817
eventListeners: {
@@ -24,78 +23,82 @@ function initSurfaceService() {
2423

2524
describe('SurfaceAnalystService', function () {
2625
var originalTimeout;
26+
var FetchRequest = SuperMap.FetchRequest;
2727
beforeEach(function () {
2828
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
2929
jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000;
30-
surfaceAnalystEventArgsSystem = null;
31-
serviceFailedEventArgsSystem = null;
3230
});
3331
afterEach(function () {
3432
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
33+
surfaceAnalystEventArgsSystem = null;
34+
serviceFailedEventArgsSystem = null;
3535
});
3636

3737
//点数据集提取等值线
3838
it('Dataset_ISOLINE', function (done) {
39-
var surfaceAnalystService = initSurfaceService();
4039
var surfaceAnalystParameters = new SuperMap.SurfaceAnalystParametersSetting({
4140
datumValue: 0,
42-
interval: 100,
43-
resampleTolerance: 0.7,
41+
interval: 2,
42+
resampleTolerance: 0,
4443
smoothMethod: SuperMap.SmoothMethod.BSPLINE,
45-
smoothness: 3
44+
smoothness: 3,
45+
clipRegion: null
4646
});
4747
var params = new SuperMap.DatasetSurfaceAnalystParameters({
4848
extractParameter: surfaceAnalystParameters,
4949
dataset: "SamplesP@Interpolation",
5050
resolution: 3000,
5151
zValueFieldName: "AVG_TMP",
52-
surfaceAnalystMethod: SuperMap.SurfaceAnalystMethod.ISOLINE
52+
surfaceAnalystMethod: SuperMap.SurfaceAnalystMethod.ISOLINE,
53+
resultSetting: new SuperMap.DataReturnOption({expectCount: 1})
5354
});
54-
55+
var surfaceAnalystService = initSurfaceService();
5556
expect(surfaceAnalystService).not.toBeNull();
5657
expect(surfaceAnalystService.url).toEqual(spatialAnalystURL);
58+
spyOn(FetchRequest, 'commit').and.callFake(function (method, testUrl, params, options) {
59+
expect(method).toBe('POST');
60+
expect(testUrl).toBe(spatialAnalystURL + "/datasets/SamplesP@Interpolation/isoline.json?returnContent=true");
61+
var expectParams = "{'resolution':3000,'extractParameter':{'clipRegion':null,'datumValue':0,'expectedZValues':null,'interval':2,'resampleTolerance':0,'smoothMethod':\"BSPLINE\",'smoothness':3},'resultSetting':{'expectCount':1,'dataset':null,'dataReturnMode':\"RECORDSET_ONLY\",'deleteExistResultDataset':true},'zValueFieldName':\"AVG_TMP\",'filterQueryParameter':{'attributeFilter':null,'name':null,'joinItems':null,'linkItems':null,'ids':null,'orderBy':null,'groupBy':null,'fields':null}}";
62+
expect(params).toBe(expectParams);
63+
expect(options).not.toBeNull();
64+
return Promise.resolve(new Response(surfaceAnalystEscapedJson));
65+
});
5766
surfaceAnalystService.processAsync(params);
5867
setTimeout(function () {
59-
try {
60-
var surfaceAnalystResult = surfaceAnalystEventArgsSystem.result;
61-
expect(surfaceAnalystResult).not.toBeNull();
62-
expect(surfaceAnalystResult.succeed).toBeTruthy();
63-
expect(surfaceAnalystResult.recordset.features.type).toEqual("FeatureCollection");
64-
var features = surfaceAnalystResult.recordset.features.features;
65-
expect(features.length).toBeGreaterThan(0);
66-
for (var i = 0; i < features.length; i++) {
67-
expect(features[i].type).toEqual("Feature");
68-
expect(features[i].id).not.toBeNull();
69-
expect(features[i].properties).not.toBeNull();
70-
expect(features[i].geometry.type).toEqual("LineString");
71-
var coordinates = features[i].geometry.coordinates;
72-
expect(coordinates.length).toBeGreaterThan(0);
73-
for (var j = 0; j < coordinates.length; j++) {
74-
expect(coordinates[j].length).toEqual(2);
75-
}
68+
var surfaceAnalystResult = surfaceAnalystEventArgsSystem.result;
69+
expect(surfaceAnalystResult).not.toBeNull();
70+
expect(surfaceAnalystResult.succeed).toBeTruthy();
71+
expect(surfaceAnalystResult.recordset.features.type).toEqual("FeatureCollection");
72+
var features = surfaceAnalystResult.recordset.features.features;
73+
expect(features.length).toBeGreaterThan(0);
74+
for (var i = 0; i < features.length; i++) {
75+
expect(features[i].type).toEqual("Feature");
76+
expect(features[i].id).not.toBeNull();
77+
expect(features[i].properties).not.toBeNull();
78+
expect(features[i].geometry.type).toEqual("LineString");
79+
var coordinates = features[i].geometry.coordinates;
80+
expect(coordinates.length).toBeGreaterThan(0);
81+
for (var j = 0; j < coordinates.length; j++) {
82+
expect(coordinates[j].length).toEqual(2);
7683
}
77-
var fieldCaptions = surfaceAnalystResult.recordset.fieldCaptions.length;
78-
expect(fieldCaptions).toBeGreaterThan(0);
79-
expect(surfaceAnalystResult.recordset.fieldTypes.length).toEqual(fieldCaptions);
80-
expect(surfaceAnalystResult.recordset.fields.length).toEqual(fieldCaptions);
81-
surfaceAnalystService.destroy();
82-
expect(surfaceAnalystService.EVENT_TYPES == null).toBeTruthy();
83-
expect(surfaceAnalystService.events == null).toBeTruthy();
84-
expect(surfaceAnalystService.lastResult == null).toBeTruthy();
85-
expect(surfaceAnalystService.eventListeners == null).toBeTruthy();
86-
done();
87-
} catch (exception) {
88-
expect(false).toBeTruthy();
89-
console.log("SurfaceAnalystService" + exception.name + ":" + exception.message);
90-
surfaceAnalystService.destroy();
91-
done();
9284
}
93-
}, 10000);
85+
var fieldCaptions = surfaceAnalystResult.recordset.fieldCaptions.length;
86+
expect(fieldCaptions).toBeGreaterThan(0);
87+
expect(surfaceAnalystResult.recordset.fieldTypes.length).toEqual(fieldCaptions);
88+
expect(surfaceAnalystResult.recordset.fields.length).toEqual(fieldCaptions);
89+
surfaceAnalystService.destroy();
90+
expect(surfaceAnalystService.EVENT_TYPES == null).toBeTruthy();
91+
expect(surfaceAnalystService.events == null).toBeTruthy();
92+
expect(surfaceAnalystService.lastResult == null).toBeTruthy();
93+
expect(surfaceAnalystService.eventListeners == null).toBeTruthy();
94+
params.destroy();
95+
surfaceAnalystService.destroy();
96+
done();
97+
}, 1000);
9498
});
9599

96100
//点数据集提取等值面
97101
it('Dataset_ISOREGION', function (done) {
98-
var surfaceAnalystService = initSurfaceService();
99102
var surfaceAnalystParameters = new SuperMap.SurfaceAnalystParametersSetting({
100103
datumValue: 70,
101104
interval: 100,
@@ -108,39 +111,39 @@ describe('SurfaceAnalystService', function () {
108111
dataset: "SamplesP@Interpolation",
109112
resolution: 3000,
110113
zValueFieldName: "AVG_WTR",
111-
surfaceAnalystMethod: SuperMap.SurfaceAnalystMethod.ISOREGION
114+
surfaceAnalystMethod: SuperMap.SurfaceAnalystMethod.ISOREGION,
115+
resultSetting: new SuperMap.DataReturnOption({expectCount: 1})
116+
});
117+
spyOn(FetchRequest, 'commit').and.callFake(function (method, testUrl, params, options) {
118+
expect(method).toBe('POST');
119+
expect(testUrl).toBe(spatialAnalystURL + "/datasets/SamplesP@Interpolation/isoregion.json?returnContent=true");
120+
var expectParams = "{'resolution':3000,'extractParameter':{'clipRegion':null,'datumValue':70,'expectedZValues':null,'interval':100,'resampleTolerance':0.7,'smoothMethod':\"BSPLINE\",'smoothness':3},'resultSetting':{'expectCount':1,'dataset':null,'dataReturnMode':\"RECORDSET_ONLY\",'deleteExistResultDataset':true},'zValueFieldName':\"AVG_WTR\",'filterQueryParameter':{'attributeFilter':null,'name':null,'joinItems':null,'linkItems':null,'ids':null,'orderBy':null,'groupBy':null,'fields':null}}";
121+
expect(params).toBe(expectParams);
122+
expect(options).not.toBeNull();
123+
return Promise.resolve(new Response(surfaceAnalysis_Dataset_ISOREGION));
112124
});
125+
var surfaceAnalystService = initSurfaceService();
113126
surfaceAnalystService.processAsync(params);
114127
setTimeout(function () {
115-
try {
116-
var surfaceAnalystResult = surfaceAnalystEventArgsSystem.result.recordset.features;
117-
expect(surfaceAnalystResult).not.toBeNull();
118-
expect(surfaceAnalystResult.features).not.toBeNull();
119-
expect(surfaceAnalystResult.type).toBe("FeatureCollection");
120-
surfaceAnalystService.destroy();
121-
params.destroy();
122-
done();
123-
} catch (exception) {
124-
console.log("SurfaceAnalystService" + exception.name + ":" + exception.message);
125-
surfaceAnalystService.destroy();
126-
params.destroy();
127-
expect(false).toBeTruthy();
128-
done();
129-
}
130-
}, 20000);
128+
var surfaceAnalystResult = surfaceAnalystEventArgsSystem.result.recordset.features;
129+
expect(surfaceAnalystResult).not.toBeNull();
130+
expect(surfaceAnalystResult.features).not.toBeNull();
131+
expect(surfaceAnalystResult.type).toBe("FeatureCollection");
132+
surfaceAnalystService.destroy();
133+
params.destroy();
134+
done();
135+
}, 1000);
131136
});
132137

133138
//对象提取等值线
134139
it('Geometry_ISOLINE', function (done) {
135-
var surfaceAnalystService = initSurfaceService();
136140
var surfaceAnalystParameters = new SuperMap.SurfaceAnalystParametersSetting({
137141
datumValue: -3,
138142
interval: 0.5,
139143
resampleTolerance: 0.7,
140144
smoothMethod: SuperMap.SmoothMethod.BSPLINE,
141145
smoothness: 3
142146
});
143-
144147
var params = new SuperMap.GeometrySurfaceAnalystParameters({
145148
extractParameter: surfaceAnalystParameters,
146149
points: [new SuperMap.Geometry.Point(-4000, 2000),
@@ -155,35 +158,38 @@ describe('SurfaceAnalystService', function () {
155158
new SuperMap.Geometry.Point(0, 0)
156159
],
157160
resolution: 3000,
158-
zValues: [-3, -2, 0, -1, -3, 0, 1, 0, 1, 1,],
161+
zValues: [-3, -2, 0, -1, -3, 0, 1, 0, 1, 1],
159162
surfaceAnalystMethod: SuperMap.SurfaceAnalystMethod.ISOLINE,
160163
resultSetting: new SuperMap.DataReturnOption({
161-
expectCount: 100
164+
expectCount: 1
162165
})
163166
});
167+
spyOn(FetchRequest, 'commit').and.callFake(function (method, testUrl, params, options) {
168+
expect(method).toBe('POST');
169+
expect(testUrl).toBe(spatialAnalystURL + "/geometry/isoline.json?returnContent=true");
170+
var expectParams = "{'resolution':3000,'extractParameter':{'clipRegion':null,'datumValue':-3,'expectedZValues':null,'interval':0.5,'resampleTolerance':0.7,'smoothMethod':\"BSPLINE\",'smoothness':3},'resultSetting':{'expectCount':1,'dataset':null,'dataReturnMode':\"RECORDSET_ONLY\",'deleteExistResultDataset':true},'surfaceAnalystMethod':\"ISOLINE\",'points':[{'id':\"SuperMap.Geometry_2\",'bounds':null,'SRID':null,'x':-4000,'y':2000,'tag':null,'type':\"Point\"},{'id':\"SuperMap.Geometry_3\",'bounds':null,'SRID':null,'x':-4500,'y':2000,'tag':null,'type':\"Point\"},{'id':\"SuperMap.Geometry_4\",'bounds':null,'SRID':null,'x':-3000,'y':3000,'tag':null,'type':\"Point\"},{'id':\"SuperMap.Geometry_5\",'bounds':null,'SRID':null,'x':-3000,'y':2000,'tag':null,'type':\"Point\"},{'id':\"SuperMap.Geometry_6\",'bounds':null,'SRID':null,'x':-2500,'y':2500,'tag':null,'type':\"Point\"},{'id':\"SuperMap.Geometry_7\",'bounds':null,'SRID':null,'x':-2000,'y':2000,'tag':null,'type':\"Point\"},{'id':\"SuperMap.Geometry_8\",'bounds':null,'SRID':null,'x':-2000,'y':3000,'tag':null,'type':\"Point\"},{'id':\"SuperMap.Geometry_9\",'bounds':null,'SRID':null,'x':-2000,'y':2000,'tag':null,'type':\"Point\"},{'id':\"SuperMap.Geometry_10\",'bounds':null,'SRID':null,'x':2000,'y':4000,'tag':null,'type':\"Point\"},{'id':\"SuperMap.Geometry_11\",'bounds':null,'SRID':null,'x':0,'y':0,'tag':null,'type':\"Point\"}],'zValues':[-3,-2,0,-1,-3,0,1,0,1,1]}";
171+
expect(params).toBe(expectParams);
172+
expect(options).not.toBeNull();
173+
return Promise.resolve(new Response(surfaceAnalysis_Geometry_ISOLINE));
174+
});
175+
var surfaceAnalystService = initSurfaceService();
164176
surfaceAnalystService.processAsync(params);
165177
setTimeout(function () {
166-
try {
167-
var surfaceAnalystResult = surfaceAnalystEventArgsSystem.result.recordset.features;
168-
expect(surfaceAnalystResult).not.toBeNull();
169-
expect(surfaceAnalystResult.features).not.toBeNull();
170-
expect(surfaceAnalystResult.type).toBe("FeatureCollection");
171-
surfaceAnalystService.destroy();
172-
params.destroy();
173-
done();
174-
} catch (exception) {
175-
console.log("SurfaceAnalystService" + exception.name + ":" + exception.message);
176-
surfaceAnalystService.destroy();
177-
params.destroy();
178-
expect(false).toBeTruthy();
179-
done();
180-
}
181-
}, 10000);
178+
expect(surfaceAnalystEventArgsSystem.type).toBe("processCompleted");
179+
expect(surfaceAnalystEventArgsSystem.result.dataset).toBeNull();
180+
expect(surfaceAnalystEventArgsSystem.result.succeed).toBe(true);
181+
var surfaceAnalystResult = surfaceAnalystEventArgsSystem.result.recordset.features;
182+
expect(surfaceAnalystResult).not.toBeNull();
183+
expect(surfaceAnalystResult.features).not.toBeNull();
184+
expect(surfaceAnalystResult.type).toBe("FeatureCollection");
185+
surfaceAnalystService.destroy();
186+
params.destroy();
187+
done();
188+
}, 1000);
182189
});
183190

184191
//对象提取等值面
185192
it('Geometry_ISOREGION', function (done) {
186-
var surfaceAnalystService = initSurfaceService();
187193
var surfaceAnalystParameters = new SuperMap.SurfaceAnalystParametersSetting({
188194
datumValue: -3,
189195
interval: 0.5,
@@ -208,32 +214,33 @@ describe('SurfaceAnalystService', function () {
208214
zValues: [-3, 0, 10, 20, 13, 8, 5, 20, 10, 15],
209215
surfaceAnalystMethod: SuperMap.SurfaceAnalystMethod.ISOREGION,
210216
resultSetting: new SuperMap.DataReturnOption({
211-
expectCount: 100
217+
expectCount: 1
212218
})
213219
});
220+
spyOn(FetchRequest, 'commit').and.callFake(function (method, testUrl, params, options) {
221+
expect(method).toBe('POST');
222+
expect(testUrl).toBe(spatialAnalystURL + "/geometry/isoregion.json?returnContent=true");
223+
expect(options).not.toBeNull();
224+
return Promise.resolve(new Response(surfaceAnalysis_Geometry_ISOREGION));
225+
});
226+
var surfaceAnalystService = initSurfaceService();
214227
surfaceAnalystService.processAsync(params);
215228
setTimeout(function () {
216-
try {
217-
var surfaceAnalystResult = surfaceAnalystEventArgsSystem.result.recordset.features;
218-
expect(surfaceAnalystResult).not.toBeNull();
219-
expect(surfaceAnalystResult.features).not.toBeNull();
220-
expect(surfaceAnalystResult.type).toBe("FeatureCollection");
221-
surfaceAnalystService.destroy();
222-
params.destroy();
223-
done();
224-
} catch (exception) {
225-
console.log("SurfaceAnalystService" + exception.name + ":" + exception.message);
226-
surfaceAnalystService.destroy();
227-
params.destroy();
228-
expect(false).toBeTruthy();
229-
done();
230-
}
231-
}, 10000);
229+
expect(surfaceAnalystEventArgsSystem.type).toBe("processCompleted");
230+
expect(surfaceAnalystEventArgsSystem.result.dataset).toBeNull();
231+
expect(surfaceAnalystEventArgsSystem.result.succeed).toBe(true);
232+
var surfaceAnalystResult = surfaceAnalystEventArgsSystem.result.recordset.features;
233+
expect(surfaceAnalystResult).not.toBeNull();
234+
expect(surfaceAnalystResult.features).not.toBeNull();
235+
expect(surfaceAnalystResult.type).toBe("FeatureCollection");
236+
surfaceAnalystService.destroy();
237+
params.destroy();
238+
done();
239+
}, 1000);
232240
});
233241

234242
//失败
235243
it('fail', function (done) {
236-
var surfaceAnalystService = initSurfaceService();
237244
var surfaceAnalystParameters = new SuperMap.SurfaceAnalystParametersSetting({
238245
datumValue: 70,
239246
interval: 100,
@@ -247,23 +254,22 @@ describe('SurfaceAnalystService', function () {
247254
resolution: 3000,
248255
zValueFieldName: "AVG_WTR"
249256
});
257+
spyOn(FetchRequest, 'commit').and.callFake(function (method, testUrl, params, options) {
258+
expect(method).toBe('POST');
259+
expect(testUrl).toBe(spatialAnalystURL + "/datasets/SamplesP1@Interpolation/isoline.json?returnContent=true");
260+
var escapedJson = "{\"succeed\":false,\"error\":{\"code\":400,\"errorMsg\":\"数据集SamplesP1@Interpolation不存在\"}}";
261+
return Promise.resolve(new Response(escapedJson));
262+
});
263+
var surfaceAnalystService = initSurfaceService();
250264
surfaceAnalystService.processAsync(params);
251265
setTimeout(function () {
252-
try {
253-
expect(serviceFailedEventArgsSystem.error.errorMsg).not.toBeNull();
254-
expect(serviceFailedEventArgsSystem.error.errorMsg).toContain("数据集SamplesP1@Interpolation不存在");
255-
expect(serviceFailedEventArgsSystem.error.code).toEqual(400);
256-
surfaceAnalystService.destroy();
257-
params.destroy();
258-
done();
259-
} catch (exception) {
260-
expect(false).toBeTruthy();
261-
console.log("SurfaceAnalystService" + exception.name + ":" + exception.message);
262-
surfaceAnalystService.destroy();
263-
params.destroy();
264-
done();
265-
}
266-
}, 2000);
266+
expect(serviceFailedEventArgsSystem.type).toBe("processFailed");
267+
expect(serviceFailedEventArgsSystem.error.errorMsg).toBe("数据集SamplesP1@Interpolation不存在");
268+
expect(serviceFailedEventArgsSystem.error.code).toEqual(400);
269+
surfaceAnalystService.destroy();
270+
params.destroy();
271+
done();
272+
}, 1000);
267273
})
268274
});
269275

0 commit comments

Comments
 (0)