我创建了DataColumn类。现在我想用Moq进行单元测试。我该如何为此编写代码?
[Serializable]
public class DataColumn<T> : IDataColumn
{
private string columnName = null;
private T defaultValue;
public DataColumn(string columnName) : this(columnName, default(T))
{
}
public DataColumn(string columnName, T defaultValue)
{
ColumnName = columnName;
DefaultValue = defaultValue;
}
public T DefaultValue
{
get
{
return defaultValue;
}
set
{
defaultValue = value;
}
}
public bool IsDefaultValueDefined()
{
return ReferenceEquals(defaultValue, default(T));
}
public string ColumnName
{
get
{
return columnName;
}
set
{
this.columnName = value;
}
}
public Type DataType
{
get
{
return typeof(T);
}
}
public override string ToString()
{
return ColumnName + "[" + DataType.Name + "]" + (IsDefaultValueDefined() ? " (" + DefaultValue + ")" : " ");
}
如何为这段代码创建单元测试?
答案 0 :(得分:1)
不确定为什么你需要Moq来测试这个,你是否试图让Moq这个类来测试另一个?
Mock<IDataColumn<T>> dc = new Mock<IDataColumn<T>>();
dc.Setup(s=>s.IsDefaultValueDefined).Returns(true);
someOtherObject.SomeMethod(dc.object);
dc.verfiy(s=>s.IsDefaultValueDefined, Times.Once());