jeudi 26 février 2015

How to separate business logic from web framework for testing?

For testing web applications, many sources suggest keeping your business logic as free from the web framework as possible to make it possible to test without needing to use the web framework. How can this be done?


If I have a registration page, I need to validate the HTTP POST data (e.g. making sure date of birth is a date and required fields are given), then I'd need to persist the validated data to the database. I'm not sure how I can separate the parts related to the web framework from the business logic, as it's quite a simple piece of functionality. Same applies to login pages, account deletion pages, etc.


I imagine the web framework wouldn't be doing much other than passing the HTTP data to the business logic function, which would then do everything (validation, persistence) that the web framework function was doing before this refactoring. So I don't see what I'm gaining. For example it would become



businessLogicRegister(
request.data['email'],
request.data['birthday'],
request.data['username']
);


Could someone give some examples of what such basic functions like registration/login might look like in a basic Node JS framework like Express or Koa and a good way to separate business logic from the web framework? And how that would help to make the code more unit-testable?





Aucun commentaire:

Enregistrer un commentaire