gpt4 book ai didi

c# - Visual Studio 单元测试生成错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:15:40 27 4
gpt4 key购买 nike

我试图让 Visual Studio 2010 为以下类创建单元测试,但是,抛出了以下错误。我研究发现这是由于在使用之前未设置引用引起的,但是,我没有看到我的代码中存在此问题的位置。

[Serializable]
class PrintUser : IEquatable<PrintUser>
{
public string Username { get; private set; }
public int PageLimit { get; set; }
public bool LimitEnforced { get; set; }

public PrintUser(string userName)
{
this.Username = userName;
}

bool IEquatable<PrintUser>.Equals(PrintUser other)
{
return this.Username == other.Username;
}
}

在尝试生成测试时,发生了以下错误:对象引用未设置到对象的实例。

更新:我解决了不检查 null 的问题,但这并没有解决问题。尝试生成测试代码时发生错误。还有另一个奇怪的错误刚刚开始发生在另一个类上。我编写了类,然后右键单击 equals 方法并选择为该方法创建一个单元测试。然后就报错了,没有生成任何测试代码。

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\TeamTest\Microsoft.TeamTest.targets(14,5): error : Signature of the body and declaration in a method implementation do not match. Type: 'PrintMonitorComponents.ADUserGroup_Accessor'. Assembly: 'PrintMonitorComponents_Accessor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.

更新:PrintUser 类(如上所示)也抛出类似的错误,如下所列。我已更新我的代码以在 equals 方法中检查 null。

Signature of the body and declaration in a method implementation do not match. Type: 'PrintMonitorComponents.PrintUser_Accessor'. Assembly: 'PrintMonitorComponents_Accessor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. PrintMonitorComponentsTest

最佳答案

我不知道为什么,但是在我将接口(interface)实现从显式更改为隐式后错误停止发生。此外,经过此更改后,Visual Studio 2010 能够正常生成单元测试/代码。

这是我再次收到的错误:

Signature of the body and declaration in a method implementation do not match. Type: 'PrintMonitorComponents.PrintUser_Accessor'. Assembly: 'PrintMonitorComponents_Accessor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. PrintMonitorComponentsTest

这是新代码:

[Serializable]
class PrintUser : IEquatable<PrintUser>
{
public string Username { get; private set; }
public int PageLimit { get; set; }
public bool LimitEnforced { get; set; }

public PrintUser(string userName)
{
this.Username = userName;
}

public bool Equals(PrintUser other)
{
if (other == null)
{
return false;
}
else
{
return this.Username == other.Username;
}
}
}

关于c# - Visual Studio 单元测试生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6643535/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com