我使用 C#、xUnit 和 Moq 进行单元测试。我有一个具有复杂类型属性的对象。要“清除”该属性及其所有属性,我这样做:
this.ComplexTypeInstance = new ComplexType();
现在我试图断言 ComplexTypeInstance 已分配给一个全新的对象。我的第一个想法是将 ComplexTypeInstance 与 new ComplexType() 进行比较,看看它们是否相等。但是要执行类似的操作,我认为我必须重写 equals 运算符。有什么方法可以轻松检查所有属性是否都设置为默认值?或者有没有办法断言我已经更新了对象?
最佳答案
Or is there a way to assert that I've newed up the object?
只需使用 ReferenceEquals
比较旧引用和新引用之间的引用。如果它们不同,则该属性已被替换。
var origInstance = this.ComplexInstance;
... // Some operation
Assert.False(object.ReferenceEquals(this.ComplexInstance, origInstance));
关于c# - 您如何测试是否已创建对象的新实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42686456/