DEV Community

[Comment from a deleted post]
Collapse
 
canderson93 profile image
Carl Anderson

Just to remove an assumptions you seem to have about testing:

  • You will never be 100% confident in your code. There is always room for strange bugs.
  • You shouldn't be choosing between unit tests and integration tests - it's a good idea to do both of them.

Unit tests can provide fast and thorough tests of small pieces of code, and if they fail they can isolate a problem to the line number. Integration tests will tell you if the entire endpoint is working, which cannot be guaranteed by unit tests alone.

In terms of mocking, it doesn't matter if your mock succeeds where production would fail - issues with the real createUser should be caught by its own tests (and you shouldn't be mocking createUser if that's what you're trying to test). You only really need 1 test to fail to show that something is wrong.

Testing requires a fair bit of back-and-forth between tests and code. Just as it's important for you to test your code accurately, it's important that your code is easy to test in the first place. Don't be afraid to refactor things to make it easier to create unit tests -- additionally, be sure to try and break the code you're testing to verify that your tests actually fail.