I have 2 classes:
- FirstDeep.cs
-
SecondDeep.cs
I did simple code for example:
class FirstDeep
{
public FirstDeep() { }
public string AddA(string str)
{
SecondDeep sd = new SecondDeep();
bool flag = sd.SomethingToDo(str);
if (flag == true)
str = string.Concat(str, "AAA");
else
str = string.Concat(str, "BBB");
return str;
}
}
and
class SecondDeep
{
public bool SomethingToDo(string str)
{
bool flag = false;
if (str.Length < 10)
{
//todo something in DB, and after that flag should be TRUE
}
return flag;
}
}
Then I want to write unit test for method “AddA”:
class Tests
{
[Test]
public void AddATest()
{
string expected = "ABCAAA";
FirstDeep fd = new FirstDeep();
string res = fd.AddA("ABC");
Assert.AreEqual(expected, res);
}
}
And after that I have trouble, I don’t know how correct write stub for method SomethingToDo in my Test class. I always have false. I should just return TRUE. But how?
A good way to allow you to write stubs is to use dependency injection.
FirstDeepdepends onSecondDeepand in your test you want to replaceSecondDeepwith a stub.First change your existing code by extracting an interface for
SecondDeepand then inject that intoFirstDeepin the constructor:Note that
FirstDeepno longer creates aSecondDeepinstance. Instead an instance is injected in the constructor.In your test you can create a stub for
ISecondDeepwhereSomethingToDoalways returns true:In the test you use the stub:
In production code you use the “real”
SecondDeep:Using a dependency injection container and a stubbing framework can make a lot of this easier to do.
If you don’t want to rewrite your code you can use a framework for intercepting calls like Microsoft Moles. In the next version of Visual Studio a similar technology will be available in the Fakes Framework.