Kin*_*han 1 c# testing unit-testing visual-studio-2010
标题为.
我有一个实现IComparable的类,需要进行排序.我针对所有方法编写了所有单元测试.但是,编写单元测试以检查它是否实现了IComparable是否有意义?
因为在UI中排序时接口不起作用.但编译它仍然有效.因此,如果我有这样的测试用例,如果有人删除了该接口,则可以捕获.
我的班级是这样的:
public class ComparableCustomType: IComparable
{
private readonly someFields;
public ComparableCustomType(AnotherBusinessObject obj)
{
//Do some parsing against the obj
}
public int CompareTo(object obj)
{
//Some custom sorting logic
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我的测试用例是:
[TestMethod]
public void CompareTo_IsImplementIComaparable()
{
IComparable comparable = Isolate.Fake.Instance<ComparableCustomType>();
Assert.AreNotEqual(null, comparable);
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是我如何使用这个属性....(或者我应该说这是该人如何使用这个属性...)
public class CustomItem{
private AnotherBusinessObject anotherBusinessObj = null
public CustomItem(AnotherBusinessObject obj)
{
this.anotherBusinessObj = obj;
}
public ComparableCustomType {
get { return new CamparableCustomType(this.anotherBusinessObj); }
}
public string SomeOtherProperty {get;set;}
publci int AnotherProperty {get;set;}
}
public ObservableCollection<CustomItem> MyCustomCollection {get;set;}
Run Code Online (Sandbox Code Playgroud)
然后这个集合将数据绑定到我的GridView ....所以它会自动生成所有列.....
为了快速总结我的答案,我说不,编写单元测试以检查类实现/继承的内容没有多大意义.IMO,应该将单元测试写入测试逻辑/功能,而不是类型.这非常类似于编写测试以确保您可以实例化构造函数.通常类似的东西是矫枉过正的,我相信这种情况也是矫枉过正的.
应在编译时检查您的代码.您可以在实例化对象时应用此约束(如果您使用对象的方式可行).
假设您实现的类IComparable被称为FooComparable:
IComparable foo = new FooComparable();
Run Code Online (Sandbox Code Playgroud)
同样,如果您已经实例化了该对象,并且它的唯一功能不一定是IComparable对象,您可以应用其他一些约束.让我们说也许你有你的FooComparable,你想在将它IComparable绑定到一个控件之前确定它,你可以这样做:
IComparable dataSource = fooObj;
Run Code Online (Sandbox Code Playgroud)
如果您尝试这个并且FooComparable没有实现IComparable,编译器会抱怨.也许您应该提供一个代码示例,说明您如何使用该类,以便我们提供更多建议.