uri*_*rig 1 c# resharper unit-testing visual-studio-2013
我有在C#中一个相当简单的GetHashCode()实现按乔恩斯基特的答案在这里.这是我的代码:
public override int GetHashCode()
{
unchecked
{
int hash = 17;
if (Title != null)
{
hash = hash * 23 + Title.GetHashCode(); // Title is of type string
}
return hash;
}
}
Run Code Online (Sandbox Code Playgroud)
当我通过Visual Studio 2013的测试资源管理器运行针对此方法的NUnit测试时,我得到哈希码的一个值,当我通过ReSharper 8的单元测试资源管理器运行它时,我获得了不同的值.
这是我的单元测试代码:
[Test]
public void GetGetHashCode_WithLinkAndTitle()
{
const int expected = -1272954771;
var target = new Article
{
Title = "Rumble News"
};
var actual = target.GetHashCode();
Assert.AreEqual(expected, actual);
}
Run Code Online (Sandbox Code Playgroud)
从VS 2013我得到实际== -1411317427,从ReSharper我得到实际== -1272954771.
为什么GetHasCode()返回的值在测试运行器中是不同的,如何使它们彼此一致?
您可能在一个测试运行器中使用32位CLR而在另一个测试运行器中使用64位CLR.两者的实施string.GetHashCode有所不同.你应该不依赖于它们正在运行之间是一致的-他们只需要为一个单一的过程中保持一致.
(对于GetHashCode从类初始化时随机初始化的静态字段中获取种子的方法是完全合理的.因此,每次运行可执行文件时,您将获得一组不同的哈希代码 - 但它们仍然在单个内部保持一致应用域名.)
| 归档时间: |
|
| 查看次数: |
139 次 |
| 最近记录: |