这绝对是主观的,但我想尽量避免它变成争论。我认为如果人们恰当地对待它,这将是一个有趣的问题。
这个问题的想法来自于我对“你最讨厌的语言的哪五件事?”问题的回答。我认为c#中的类在默认情况下应该是密封的——我不会把我的理由放在这个问题上,但我可能会写一个更完整的解释来回答这个问题。我对评论中的讨论热度感到惊讶(目前有25条评论)。
那么,你有什么有争议的观点?我宁愿避免那些基于相对较少的基础而导致相当宗教的事情(例如,大括号放置),但例如可能包括“单元测试实际上并没有多大帮助”或“公共字段确实是可以的”之类的事情。重要的是(至少对我来说)你的观点背后是有理由的。
请提出你的观点和理由——我鼓励人们投票给那些有充分论证和有趣的观点,不管你是否恰好同意这些观点。
缺陷和增强请求是相同的
Unless you are developing software on a fixed-price contract, there should be no difference when prioritizing your backlog between "bugs" and "enhancements" and "new feature" requests. OK - maybe that's not controversial, but I have worked on enterprise IT projects where the edict was that "all open bugs must be fixed in the next release", even if that left no developer time for the most desirable new features. So, a problem which was encountered by 1% of the users, 1% of the time took precedence over a new feature would might be immediately useful to 90% of the users. I like to take my entire project backlog, put estimates around each item and take it to the user community for prioritization - with items not classified as "defect", "enhancement", etc.
当为数据访问层创建单元测试时,数据应该直接从DB中检索,而不是从模拟对象中检索。
考虑以下几点:
void IList<Customer> GetCustomers()
{
List<Customer> res = new List<Customer>();
DbCommand cmd = // initialize command
IDataReader r = cmd.ExecuteQuery();
while(r.read())
{
Customer c = ReadFiledsIntoCustomer(r);
res.Add(c);
}
return res;
}
在GetCustomers的单元测试中,对cmd.ExecuteQuery()的调用应该实际访问DB还是应该模拟它的行为?
我认为,如果以下情况成立,您不应该模拟对DB的实际调用:
存在一个测试服务器和模式。
模式是稳定的(意味着您不期望对其进行重大更改)
DAL没有聪明的逻辑:查询是简单地构造的(config/stored procs)
去虹膜化的逻辑很简单。
从我的经验来看,这种方法的最大好处是你可以尽早与DB进行交互,体验“感觉”,而不仅仅是“外观”。它为您省去了许多事后的麻烦,并且是熟悉模式的最佳方法。
许多人可能会争辩说,一旦执行流跨越了进程边界,它就会成为一个单元测试。我同意它有它的缺点,特别是当数据库不可用,然后你不能运行UT。
然而,我相信在许多情况下这应该是一件有效的事情。