Visual Studio Unit Test Generation Error
I tried to have Visual Studio 2010 create unit tests for the following class, however, the following error was thrown. I researched and found that this is caused by a reference not being set before it is used, however, I do not see where this problem exists in my code.
[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;
}
}
While trying to generate your tests, the following errors occurred: Object reference not set to an inst开发者_JAVA百科ance of an object.
UPDATE: I fixed the problem of not checking for null, however that did not solve the problem. The error occurs when trying to generate the test code. There is also another strange error that just started occurring on another class. I wrote the class, and then right clicked on the equals method and chose to create a unit test for just that method. Then, the error occurred and there was not any test code generated.
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'.
UPDATE: The PrintUser class (shown above) is also throwing a similar error which is listed below. I have updated my code to check for null in the equals method.
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
I have no idea why, but the error stopped occurring after I changed the interface implementation from explicit to implicit. Also, after this change, Visual Studio 2010 is able to generate unit tests/code normally.
Here is the error that I was receiving again:
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
Here is the new code:
[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;
}
}
}
You need to check that other
isn't null
in your Equals
method.
精彩评论