tsukimi2 / vidbits

0 stars 0 forks source link

Summary #1

Open mp1pro opened 5 years ago

mp1pro commented 5 years ago

The route, database and user interaction tests are all correct. running npm test showed that all the tests ran and passed successfully. Did not find any errors. The features on the app also worked as it should. The written tests covered all the features to be tested. Great job.

tsukimi2 commented 5 years ago

Thanks for the review. Actually, I have some further questions about testing in general. Hope you can help me with them or direct me to the correct venue of asking such questions in CodeAcademy. Thanks.

1) How do you unit test a middleware function? For example, if I have a middleware function as below where next() is within an if condition that doesn't always get called, how should I write a unit test for that?

// Middleware for enforcing router to to route through https. Yeah, it can be done simply // by configuring webserver to do the same thing instead, but just for the sake of an example. export default function(req, res, next) { if (req.headers['x-forwarded-proto'] !== 'https') { if (req.method === 'GET') { return res.redirect(301, https://${req.hostname}${req.url}) } else { throw new BadRequestError } } next() }

2) The course teaches testing through a top-down approach (i.e. feature testing first, then server testing, then model testing). Does a bottom-up approach work also? (i.e. writing unit tests covering individual functions and modules first, then integration tests and then end2end tests), or is this approach outdated already??

3) In server tests (i.e. testing your router APIs), would it be better to mock any database access instead of running database queries against an actual database, so that tests will run faster and ensure that you are testing the application code instead of testing the database at this stage? And leave the actual running against an actual database and other 3rd party library codes to the end-to-end tests (i.e. feature tests)?

mp1pro commented 5 years ago
  1. Testing the middleware is still new and there is no defacto way to do so. Here is an example; https://medium.com/@morrissinger/unit-testing-express-middleware-behavior-in-ecmascript-2015-f1641ebb8040

  2. Top-down approach is better because we can catch a model error while testing the UI. However, testing the model first would not reveal a UI error.

  3. We would want to hit the database for testing in case there is a technical flaw with the database.

For further help, contact a TDD adviser/moderator at http://codecademy.com