我有两个类:
- FirstDeep.cs
SecondDeep.cs
我举了一个简单的例子:
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;
}
}
还有:
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;
}
}
然后我想为方法“AddA”编写单元测试:
class Tests
{
[Test]
public void AddATest()
{
string expected = "ABCAAA";
FirstDeep fd = new FirstDeep();
string res = fd.AddA("ABC");
Assert.AreEqual(expected, res);
}
}
在那之后我遇到了麻烦,我不知道如何正确编写Test类中SomethingToDo方法的存根。我经常得到false的返回值,但我只想要返回TRUE。但是该怎么做呢?
bool expected = false; SecondDeep sd = new SecondDeep(); bool actualResult = sd.SomethingToDo("ABC"); Assert.AreEqual(excpected, actualResult);
...!?如果这不能满足你的需求,你应该考虑详细阐述和改进你的问题! - user57508