Wiredcraft / test-backend

39 stars 76 forks source link

Test Backend Submission - Jonathan Dai #53

Closed agreatfool closed 4 years ago

agreatfool commented 4 years ago

Hi Xavier,

I have finished the backend test, and made a pull request. Please have a look. And also document could be found here: README.md.

Best Regards, Jonathan Dai

MiffyLiye commented 4 years ago

Hi Jonathan, I have some questions about the architecture and the test strategy.

  1. In the code, there are folders like controller, dao, model, database, what is the responsibility of each layer? Unlike traditional MVC framework, this architecture doesn't have service layer, and controller layer calls dao layer directly, Can you compare this approach with skinny controller (fat model) approach, and service layer approach?

  2. In the test code, there are some server API tests, with test data prepared in a global before section with stubs. Can you compare this approach with preparing test data in each test case (in describe.before or describe.it section) approach? Can you compare the benefit & cost of using real Redis service in test cases without stubs?

  3. About the test scenarios in test cases, most of them are calling one API and assert the response, but the API specifications are more than API responses, e.g., user created with POST /user should be retrieved from GET /user/:id. Can you design a test strategy that has good support for this kind of specifications?

  4. Similar to the 3rd question, many assertions in test cases are more about format, less assertions on business meaning, e.g. GET /usergeo/nearby I would expect the test case to prepare two users, one in the range, the other out of the range, when get nearby users, the assertion assets one user is in the response, and the other user is not in the response. Can you design a test strategy that has good support for assertions with more business meaning?

agreatfool commented 4 years ago

Hi @MiffyLiye, Let me answer the questions one by one.

Question 1:

Functionality of each layer:

Let me explain why there is no service layer in codes. Since this test is a small test, there is almost no business logic here. So I omitted the service layer according to the rule "quick and dirty". In other projects of mine, including office projects and side projects, there are service layer existing.

For your question, I think skinny controller fat model (or service approach) is better than leaving logics all in the controller layer. Controller shall only accept requests and validate them, then move the valid requests into model layer, this could separate duties very clearly.

For the choice between skinny controller fat model / service approach. If business logic is not too complex, it's good to use fat model, just move domain logic into model layer directly. And if the project or system is very complex, it's good to use service approach (n-tiers arch), separate domain logic into services. And I prefer service approach in the daily work (If the project I can control), since I think this approach is more clear and flexible.


Let me explain a bit about the unit test part. As you know the test project is a bit simple and lack of real business logic. So my concept of unit test part is: Using correct skills/libraries and make simple unit test codes to show you guys how I do unit test in other projects. And as you can see, the logic of test case maybe a bit too simple as you mentioned in questions.


Question 2:

As I mentioned above, I thought about the logic part too simplistically. In real case, preparing global data and preparing data for single test case are all possilbe to happen. Difference:

Using real Redis service in test:


Question 3 and 4:

I have added two spec as Question 3 and Question 4 asked. Please have a look at:

xavierchow commented 4 years ago

Thanks all, this is good enough for our evaluation, closing the PR and we will get back to you soon @agreatfool .