Unfortunately, that test is incorrect and it's very fragile.
The test has to pass if you change implementation. So, if I change class NewsletterSubscriptionService to another one, a method handle to something else then the test will fail, whenever an email is saved correctly.
I do agree that this test can be seen as being fragile, but this example is just a simple one that we can use to explain the concept of dependency injection and mocking. In a real-life scenario, I'd have used an interface instead to decouple the code so that we could switch out the implementation further down the line. I didn't want to throw too much at the readers at once though and scare them away haha :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Unfortunately, that test is incorrect and it's very fragile.
The test has to pass if you change implementation. So, if I change class
NewsletterSubscriptionService
to another one, a methodhandle
to something else then the test will fail, whenever an email is saved correctly.Hi Marcin, thanks for the comment.
I do agree that this test can be seen as being fragile, but this example is just a simple one that we can use to explain the concept of dependency injection and mocking. In a real-life scenario, I'd have used an interface instead to decouple the code so that we could switch out the implementation further down the line. I didn't want to throw too much at the readers at once though and scare them away haha :)