I have some context in code to be switched depending on whether it is running under test or release. Say in my product coding:
PublishRequest(); // the real one
//PublishRequestPsudo(); // the one want to be run during unit test
The way I am thinking about is make a TestFlag class:
if (!TestFlag.PublishFlag)
{
PublishRequest();
}
else
{
PublishRequestPsudo();
}
This seems verbose if I have many place to do that. Is there any good pattern to do it?
Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.
Using Preconditions to Avoid Coupling A precondition hides all the implementation details we need to mock out from the test.
If you want true Unit Tests, then you have to mock the cache: write a mock object that implements the same interface as the cache, but instead of being a cache, it keeps track of the calls it receives, and always returns what the real cache should be returning according to the test case.
The need to test protected interfaces means that unit tests should belong to the same package as the production classes they test, so they must have the same directory path. This can be done by creating separate but parallel hierarchies for the production and test code.
A pretty good way to acheive the same is Dependency Injection/Inversion Of Control
Another good resource on this is Caching ArchitectureTestability, Dependency Injection and Multiple Providers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With