下面有一些代码在集成环境中抛出异常,但在我的单元测试中没有。基本上,我是按属性值对一些xml元素(linq-2-sql xelement)进行排序。所有节点都定义了属性。
IEnumerable<XElement> elements = ...; // elementes are of the form<recipe name="something">
elements.OrderBy(e => e.Attribute("name"))
引发的异常是“至少有一个对象必须实现IComparable”。代码可以固定为在以下任一情况下工作:
IEnumerable<XElement> elements = ...; // elementes are of the form<recipe name="something">
elements.OrderBy(e => e.Attribute("name").Value)
但我想知道为什么在调试环境中运行时会抛出异常,而不是从我的单元测试中抛出异常?我担心我的测试库使用的一些实用程序会产生意想不到的副作用,但是我找不到任何东西。我该找什么?
注意,在测试环境中,elements.first().attribute(“name”)不为空,但elements.first().attribute(“name”)as icomparable为空,因此在这两种情况下,xattribute都不实现icomparable。