【发布时间】:2008-11-20 14:47:28
【问题描述】:
我一直在为只能接受和返回字符串的 COM 对象开发一个包装器。 COM 对象的接口如下所示:
interface IMapinfo
{
void Do(string cmd);
string Eval(string cmd);
}
现在我已经制作了包含如下基本功能的类:
public class Table
{
IMapinfo MI;
public string Name
{
//pass the command to the COM object and get back the name.
get{return MI.Eval("TableInfo(1,1")");}
}
}
现在我想对这些类进行单元测试,而不必每次都创建真正的 COM 对象,设置世界然后运行测试。所以我一直在研究使用模拟对象,但我对如何在这种情况下使用模拟有点困惑。
我打算使用起订量,所以我这样写了这个测试:
[Test]
public void MockMapinfo()
{
Moq.Mock<Table> MockTable = new Moq.Mock<Table>();
MockTable.ExpectGet(n => n.Name)
.Returns("Water_Mains");
Table table = MockTable.Object;
var tablename = table.Name;
Assert.AreEqual("Water_Mains", tablename,string.Format("tablename is {0}",tablename));
Table d = new Table();
}
这是模拟我的 COM 对象的正确方法吗?发送到 eval 函数的字符串如何正确?还是我做错了?
【问题讨论】:
标签: c# unit-testing com moq mocking