My 2 cents ... how will the client know if he deleted the job or not? How should a “client” interact with this object? Are customers going to insert a stack into the history tracker? Treat the test as a different user / consumer / client of the subject. Using the same interaction as in real production. I have not heard a single rule that you are not allowed to call several methods for the tested object.
To simulate, the stack is not empty. I just call Add - 99%. I would refrain from destroying the encapsulation of this object. Handle objects like people (I think I read this in Object Thinking). Tell them to do things .. do not enter and do not enter.
eg. If you want someone to have money in their wallet,
- an easy way is to give them money and let them internally put it in their wallet.
- throw away your wallet and write in a wallet in your pocket.
I like Option1. Also see how it frees you from implementation details (which cause fragility in tests). Let's say tomorrow a person decides to use an online wallet. The latter approach will violate your tests - they will need to be updated in order to click the online wallet now, even if the object's behavior is not violated.
Another example I saw is testing Repository.GetX (), where people enter the database to enter SQL records now in the unit test .. where it would be much cleaner and easier to call the .AddX repository ( x). Isolation is desirable, but not so much that it redefines pragmatism.
I hope I have not become too strong here. It just pains me to see that the object's APIs are “distorted for validation” to such an extent that they no longer look like the “simplest thing that can work.”
source share