Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建假对象以模拟GetHashCode_C#_Visual Studio 2012_Microsoft Fakes - Fatal编程技术网

C# 创建假对象以模拟GetHashCode

C# 创建假对象以模拟GetHashCode,c#,visual-studio-2012,microsoft-fakes,C#,Visual Studio 2012,Microsoft Fakes,我有以下的类(不相关的部分删除) 更新 我解决了我的问题,但没有回答我的原始问题,所以我只是在原始问题上添加我的解决方案,以防其他人有解决方案。我没有模仿任何物体就解决了这个问题。这是一个“duh”时刻,而不是根据硬编码值设置期望值,而是根据GetHashCode的结果设置期望值 // Arrange const string nodeName = "guid"; XElement element = new XElement("Record"); int expected = element

我有以下的类(不相关的部分删除)


更新

我解决了我的问题,但没有回答我的原始问题,所以我只是在原始问题上添加我的解决方案,以防其他人有解决方案。我没有模仿任何物体就解决了这个问题。这是一个“duh”时刻,而不是根据硬编码值设置期望值,而是根据
GetHashCode
的结果设置期望值

// Arrange
const string nodeName = "guid";
XElement element = new XElement("Record");
int expected = element.GetHashCode();

// Act
XElementComparer target = new XElementComparer(nodeName);
int actual = target.GetHashCode(element);

// Assert
Assert.AreEqual(expected, actual);
然后测试其他条件

// Arrange
const string nodeName = "guid";
string attributeValue = Guid.NewGuid().ToString();
XElement element = new XElement("Record", new XAttribute(nodeName, attributeValue));
int expected = attributeValue.GetHashCode();

// Act
XElementComparer target = new XElementComparer(nodeName);
int actual = target.GetHashCode(element);

// Assert
Assert.AreEqual(expected, actual);

它并不理想,因为它没有模仿GetNullableAttrText扩展方法,但是,依我看,它比使用赝品更干净。

什么是“o.GetHashCode01”-不应该是“o.GetHashCode”。StubObject创建GetHashCode01(Func类型),这是一个允许您剔除GetHashCode方法的属性。
// Arrange
const string nodeName = "guid";
XElement element = new XElement("Record");
int expected = element.GetHashCode();

// Act
XElementComparer target = new XElementComparer(nodeName);
int actual = target.GetHashCode(element);

// Assert
Assert.AreEqual(expected, actual);
// Arrange
const string nodeName = "guid";
string attributeValue = Guid.NewGuid().ToString();
XElement element = new XElement("Record", new XAttribute(nodeName, attributeValue));
int expected = attributeValue.GetHashCode();

// Act
XElementComparer target = new XElementComparer(nodeName);
int actual = target.GetHashCode(element);

// Assert
Assert.AreEqual(expected, actual);