First of all, as you mentioned, you can replace dependencies with mocks. The code you posted doesn't contain much in terms of dependencies, but let it pretend it does.
You are testing the IsStringEmptyOrNull method.
Suppose this method belongs to a class called Foo (I'm just doing something like this). I will also change the method.
public class Foo { private IBar _bar; public Foo(IBar bar) { _bar = bar; } public static bool IsStringEmptyOrNull(string strValue) {
Here you can see that the Foo class has a panel that is introduced into the constructor. In addition, it is used in your method. If you do not want your test to actually call this method:
- because it calls db
- since it calls the file system
- you want to isolate the code from the method, from other external code (in this case ReadFromFileSystemOrDatabase ()
You can then use the layout to accomplish this. Here's how you do it:
What will happen is that when your test runs, a mock version of the dependency (in this case Bar) will be assigned to the class.
When the method calls this layout, if you have the installer to return a value, it will return that value.
In this way, you can ignore dependencies in order to focus your tests or make fun of calls to db or the file system.
As for the exact example that you mentioned, there is nothing to taunt. Your exact example is not applicable for using mocks.
Mocks is not something you would do for all of your tests.
source share