我正在使用VS2012内置的Fakes框架。
此设计问题与以下帖子有关:Unit test won't "cover" simple get method. (c#)
我不确定如何为“IDbAccess”设计类来成功“伪造”命中数据库。
Ex代码(取自上一篇文章):
public class Foo : IFoo
{
IDbAccess db;
public Foo(IDbAccess db)
{
this.db = db;
}
public Dictionary<string,string> GetSomething(string xyz)
{
var result = new Dictionary<string,string>
db.commandText = "text..."
db.connection = conn;
result.Add(db.MethodWhatever(xyz));
return result;
}
}
防爆测试方法:
[TestMethod()]
public void GetSomething()
{
var dba = new StubIDbAccess();
var target = new Foo(dba);
var expected = new Dictionary<string, string>
{
{"blahKey","blahValue"}
};
// get something
var results = target.GetSomething("xyzstring");
// verify results
var actual = results.whatever;
CollectionAssert.AreEqual(expected,actual);
}
为了“存根”IDbAccess,IDbAccess需要是一个继承自IDbCommand的类。我只是不确定如何实现它以避免必须覆盖IDbCommand中的所有内容。
public interface IDbAccess : IDbCommand
{
...?
}