How to run SAPUI5 Fiori application with mock data

我们两清 提交于 2019-12-12 03:37:18

问题


I have a Fiori application in my local PC and I see it has a model folder with a bunch of json files with mock data. I would like to run this app in a browser with this mock data but cannot find any documentation on how to do this. Do you know what is the parameter sapui5 is expecting to read data locally instead of from Odata server?

I am not using SAP Web IDE, (which i know has an option "run with mock data") but nowhere it is said what this does behind the scenes.

Thank you very much. Best regards,


回答1:


with SAP Web IDE, the MockServer (which is injected when you select the option "run with mock data") is automatically started. But you can also do this manually inside your application, we even recommend this do also be able to run automated tests with your application running in mock mode.

You can check the brandnew SAPUI5 walkthrough (step 27) for more details on how to start the MockServer from inside the application: https://github.com/SAP/openui5/tree/master/src/sap.m/test/sap/m/demokit/tutorial/walkthrough/27

The documentation for the walkthrough will only be released with OpenUI5 release 1.30 but I copied the relevant parts here, hope it helps! Michael

Documentation for the start page (testService.html): "We modify our copy of the index.html file and add a new page title to distinguish it from the productive start page. Since we clearly want to separate test and productive coding, we add a second namespace sap.ui.demo.wt.test that points to the test folder and is only used in the testService.html file.

Additionally, we switch the initialization of the component to the sap.ui.require syntax, because we do now load more additional files required for the startup of our app. The first dependency is a file called server.js that will be located in the test/service folder. We also replace the Shell and the ComponentContainer reference with the dependencies that the require statement provides.

The new artifact that we just loaded and are about to implement is our local test server that is immediately called with init method before we actually define the component. This way we can catch all requests that would go to the “real” service and process it locally by our test server when launching the app with the testService.html file."

Documentation for the mock server script: "Now that we have extended our test data with the metadata.xml file we can write the code to initialize the server that will simulate the requests instead of the real server.

We load the MockServer module as a dependency and create a helper object that defines an init method to start the server. This method is called before the component initialization in the testService.html file above.

The init method creates a MockServer instance with the same URL as the real service. The URL in configuration parameter rootURI will now be served by our test server instead of the real service. Next we set two global configuration settings for all MockServer instances that tell the server to respond automatically and introduce a delay of one second to imitate a typical server response time. Otherwise, we would have to call the respond method on the MockServer manually to simulate the call.

In order to simulate a manual backend call we can simply call the simulate method on the MockServer instance with the path to our newly created metadata.xml. This will read the test data from our local file system and set up the URL patterns that will mimic the real service.

Finally, we call start on oMockServer. From this point, each request to the URL pattern rootURI will be processed by the MockServer. If you switch from the index.html file to the testService.html file in the browser, you can now see that the test data is displayed from the local sources again, but with a short delay.

This approach is perfect for local testing, even without any network connection. This way your development does not depend on the availability of a remote server, i.e. to run your tests. From this point you have two different entry pages: One for the real “connected” app (index.html) and one for local testing (testService.html)."



来源:https://stackoverflow.com/questions/30645189/how-to-run-sapui5-fiori-application-with-mock-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!