There’s no way to test this function without having a database connection available at the time of testing. If a new developer clones the project they will need to set up a database or else tests will fail.
Unit tests are unbelievably important to us as developers because they allow us to demonstrate the correctness of the code we’ve written. More importantly, unit tests allow us to make updates to our code base with the confidence that we haven’t broken anything. In our eagerness to get 100% code coverage, however, we often write tests for logic that perhaps we have no business testing. I’m here to assert that creating mock database abstractions in order to write unit tests is a bad idea almost all of the time.
How do scientists demonstrate that a drug or vaccine is effective? Putting it to the test. It's your only choice. And, if you're reasonable, you'd never use medications that haven't been thoroughly tested. So, why don't you test software as thoroughly as you should?
Unit Testing Is NOT The Same As Integration Testing. The foundation of software resilience are integration and unit tests. Let's look at the distinctions between the two today, as well as when you would choose one over the other.
The shift towards microservices and modular applications makes testing more important and more challenging at the same time. Learn more here.
What are Programmer tests? See a clear example of each kind.
Another look into unit testing and integration testing. Unit Testing and Integration Testing are the initial and most important stages of testing. Apart from these there are some other phases in testing.