Reputation: 304
I get following AssertFailedException and can't see, why this happens.
CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of <FileExporter.Document>. The actual collection contains 0 occurrence(s).
Code:
[TestMethod]
public void GetDocumentsTest()
{
List<Document> testDocuments = new List<Document>() {
new Document(){ Path = "TestDoc1", Language = "DEU" }
, new Document(){ Path = "TestDoc2", Language = "ENG" }
};
string sqlCommand = "SELECT Path, LanguageCode FROM Document WITH(NOLOCK) ORDER BY Id";
string connectionString = "Data Source=|DataDirectory|\\Documents.sdf;Persist Security Info=False;";
List<Document> documents = new DataAccessManagerTestMockup().GetDocuments(sqlCommand, connectionString);
// Sql gets same documents with same path and same language
CollectionAssert.AreEquivalent(testDocuments, documents);
}
Update:
namespace FileExporter
{
public class Document
{
public string Path { get; set; }
public string Language { get; set; }
public bool Equals(Document y)
{
if (object.ReferenceEquals(this, y))
return true;
if (object.ReferenceEquals(this, null) || object.ReferenceEquals(y, null))
return false;
return this.Path == y.Path && this.Language == y.Language;
}
}
}
Update 2:
I think i shouldn't spend a night in cologne (3 hours driving time) and try to code. Colleague just said me i should override equals and use an object instead of Document.
namespace FileExporter
{
public class Document
{
public string Path { get; set; }
public string Language { get; set; }
public override bool Equals(object y)
{
if (object.ReferenceEquals(this, y))
return true;
if (object.ReferenceEquals(this, null) || object.ReferenceEquals(y, null))
return false;
return this.Path == ((Document) y).Path && this.Language == ((Document) y).Language;
}
public override int GetHashCode()
{
if (this == null)
return 0;
int hashCodePath = this.Path == null ? 0 : this.Path.GetHashCode();
int hashCodeLanguage = this.Language == null ? 0 : this.Language.GetHashCode();
return hashCodePath ^ hashCodeLanguage;
}
}
Upvotes: 1
Views: 1171
Reputation: 273844
For this to succeed your Document class needs to implement (override) Equals()
You can verify this easily:
var d1 = new Document(){ Path = "TestDoc1", Language = "DEU" };
var d2 = new Document(){ Path = "TestDoc1", Language = "DEU" };
Assert.AreEqual(d1, d2);
Upvotes: 1