我有一个应用程序,我使用标准内置模拟服务器UI5
运行它以进行测试。UI5
以下是 mockserver.js 中Web IDE
自动生成的代码:
sap.ui.define([
"sap/ui/core/util/MockServer",
"sap/ui/model/json/JSONModel",
"sap/base/util/UriParameters",
"sap/base/Log"
], function (MockServer, JSONModel, UriParameters, Log) {
"use strict";
var oMockServer,
_sAppPath = "de/cimt/test/",
_sJsonFilesPath = _sAppPath + "localService/mockdata";
var oMockServerInterface = {
/**
* Initializes the mock server asynchronously.
* You can configure the delay with the URL parameter "serverDelay".
* The local mock data in this folder is returned instead of the real data for testing.
* @protected
* @param {object} [oOptionsParameter] init parameters for the mockserver
* @returns{Promise} a promise that is resolved when the mock server has been started
*/
init : function (oOptionsParameter) {
var oOptions = oOptionsParameter || {};
return new Promise(function(fnResolve, fnReject) {
var sManifestUrl = sap.ui.require.toUrl(_sAppPath + "manifest.json"),
oManifestModel = new JSONModel(sManifestUrl);
oManifestModel.attachRequestCompleted(function () {
var oUriParameters = new UriParameters(window.location.href),
// parse manifest for local metatadata URI
sJsonFilesUrl = sap.ui.require.toUrl(_sJsonFilesPath),
oMainDataSource = oManifestModel.getProperty("/sap.app/dataSources/mainService"),
sMetadataUrl = sap.ui.require.toUrl(_sAppPath + oMainDataSource.settings.localUri),
// ensure there is a trailing slash
sMockServerUrl = /.*\/$/.test(oMainDataSource.uri) ? oMainDataSource.uri : oMainDataSource.uri + "/";
// ensure the URL to be relative to the application
sMockServerUrl = sMockServerUrl && new URI(sMockServerUrl).absoluteTo(sap.ui.require.toUrl(_sAppPath)).toString();
// create a mock server instance or stop the existing one to reinitialize
if (!oMockServer) {
oMockServer = new MockServer({
rootUri: sMockServerUrl
});
} else {
oMockServer.stop();
}
// configure mock server with the given options or a default delay of 0.5s
MockServer.config({
autoRespond : true,
autoRespondAfter : (oOptions.delay || oUriParameters.get("serverDelay") || 500)
});
// simulate all requests using mock data
oMockServer.simulate(sMetadataUrl, {
sMockdataBaseUrl : sJsonFilesUrl,
bGenerateMissingMockData : true
});
var aRequests = oMockServer.getRequests();
// compose an error response for each request
var fnResponse = function (iErrCode, sMessage, aRequest) {
aRequest.response = function(oXhr){
oXhr.respond(iErrCode, {"Content-Type": "text/plain;charset=utf-8"}, sMessage);
};
};
// simulate metadata errors
if (oOptions.metadataError || oUriParameters.get("metadataError")) {
aRequests.forEach(function (aEntry) {
if (aEntry.path.toString().indexOf("$metadata") > -1) {
fnResponse(500, "metadata Error", aEntry);
}
});
}
// simulate request errors
var sErrorParam = oOptions.errorType || oUriParameters.get("errorType"),
iErrorCode = sErrorParam === "badRequest" ? 400 : 500;
if (sErrorParam) {
aRequests.forEach(function (aEntry) {
fnResponse(iErrorCode, sErrorParam, aEntry);
});
}
// custom mock behaviour may be added here
// set requests and start the server
oMockServer.setRequests(aRequests);
oMockServer.start();
Log.info("Running the app with mock data");
fnResolve();
});
oManifestModel.attachRequestFailed(function () {
var sError = "Failed to load application manifest";
Log.error(sError);
fnReject(new Error(sError));
});
});
},
/**
* @public returns the mockserver of the app, should be used in integration tests
* @returns {sap.ui.core.util.MockServer} the mockserver instance
*/
getMockServer : function () {
return oMockServer;
}
};
return oMockServerInterface;
});
我的 oData 元数据中的所有集合都具有ID
整数类型!(在真实网关服务器中自动增量)
有趣的是,当我创建一个集合的新对象时,它将分配9
为第一次创建的对象的 id,第二个创建的对象将是416
等等。
很明显,内置模拟服务器使用随机生成算法,不带或带静态种子。这就是它会为我的元数据模型中的每个集合始终生成相同的 id 链的原因。
现在我的问题是如何改变 UI5 模拟服务器的这种行为?
换句话说,如何将随机数设置为模拟服务器的种子,或者如何强制它对 id 使用增量行为。
作为 id生成的 UI5 的默认行为的问题9, 416, 6671, 2631, ...
是,其中一个集合已经有一个带有 id 的项目9
!然后通过创建一个新项目,我将在我的列表中有两个具有相同 ID(即 9)的项目!