starsz commented on pull request #1245: URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765262758
> > > > > Hi, @Jaycean. Need unit test. > > > > > > > > > > > > At present, the function interface needs to export the existing route data and ensure the correctness of the exported data, so I think E2E should be written more perfectly. I think if the simple unit test only tests whether the interface can export data, it does not represent whether the function of the interface is correct, and if the relevant route data is not created, the interface will not work normally use. > > > > > > > > > Not agree with you. > > > First of all, unit testing is very important. It helps find bugs easily and quickly if someone changes the code.And unit testing can cover more code than e2e test. > > > For example, in this case. > > > We need to test these situations at least: > > > > > > 1. route can't found > > > 2. Uris contain "*" > > > 3. plugins with `request-validation` > > > 4. different route method > > > ... > > > > > > > and if the relevant route data is not created, the interface will not work normally use. > > > > > > > > > We can mock data in etcd. > > > See https://github.com/apache/apisix-dashboard/blob/master/api/internal/handler/server_info/server_info_test.go for more details. > > > > > > @starsz Now I have encountered a problem in unit testing, exporting the data that needs to create route, upstream and service. After reading the example you gave me, I still don't know how to write it. Can you give me some advice. Thks OK. Sorry, we don't have a doc to introduce the test contribute doc yet. First, you should know the mock technology. You can reference the doc: https://github.com/stretchr/testify#mock-package. Mock provides a way to check input and give the return value that we want. In your code, we should mock those function: ``` routeStore.Get routeStore.List upstreamStore.Get serviceStore.Get ``` Second, IMO, you should write two testcases One test case for `ExportRoutes`, like: `TestDataLoader_ExportRoutes` And another test case for `ExportAllRoutes`.like `TestDataLoader_ExportAllRoutes` Third, in those test case function.We should mock the store like: ``` routeMockStore := &store.MockInterface{} routeMockStore.On("Get", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { id := args.Get(0).(string) // here we can do some check on the id. }).Return(tc.giveRet, tc.giveErr) ``` Mock.Anything stands for the args when you called routeMockStore.Get. tc.giveRet and tc.giveErr means what you want to get when called this line: https://github.com/apache/apisix-dashboard/pull/1245/files#diff-195642dee38f4cdcc68921704b4b59dce5afc67d06074c1bc53004eb9b5dfc5eR70 The same as other mock functions. Any feedback is welcome. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org