【发布时间】:2012-05-16 18:02:36
【问题描述】:
我有 2 节课:
- 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);
}
}
在那之后我遇到了麻烦,我不知道在我的测试类中为方法 SomethingToDo 编写存根有多正确。我总是假的。我应该返回 TRUE。但是怎么做呢?
【问题讨论】:
-
您仍然可以使用您的模式:介绍
bool expected = false; SecondDeep sd = new SecondDeep(); bool actualResult = sd.SomethingToDo("ABC"); Assert.AreEqual(excpected, actualResult);...!?如果这不能满足您的需求,您可能会考虑详细说明和改进您的问题! -
你调试过你的代码吗?如果它发生在数据库中,我们无法为您提供帮助,因为我们没有关于那里发生的事情的详细信息。
-
是的,我调试了我的代码,并在其中写道:“//在 DB 中做一些事情,之后该标志应该为 TRUE”我使用 .NET 中的类 MembershipUser 并且此方法无法连接到 DB,这就是为什么我应该在这种情况下返回 true。