2014-09-26 17 views
0

我正在編寫一個EFException轉換器,並且正在爲它構建一個Rulset。我想驗證建設者返回的規則列表:在Nunit中,比較兩個對象列表,使它們包含相同的類型

[Test] 
    public void Build_CreateListOfEntityRules() 
    { 
     //arrange 
     var expected = new List<IEntityRule> 
          { 
           new AutomaticDataLossEntityRule(), 
           new CommitFailedEntityRule(), 
           new DbEntityValidationEntityRule(), 
           new DbUnexpectedValidationEntityRule(), 
           new DbUpdateConcurrencyEntityRule(), 
           new DbUpdateEntityRule(), 
           new EntityCommandCompliationEntityRule(), 
           new EntityCommandExecutionEntityRule(), 
           new EntityErrorEntityRule(), 
           new EntitySqlEntityRule(), 
           new InvalidOperation(), 
           new MigrationEntityRule(), 
           new MigrationsPendingEntityRule(), 
           new ModelValidationEntityRule(), 
           new ObjectNotFound(), 
           new PropertyConstraintEntityRule(), 
           new UnintentionalCodeFirstEntityRule(), 
           new UpdateEntityRule() 
          }; 

     //act 
     var actual = sut.Build(); 

     //assert 
     CollectionAssert.AreEquivalent(expected, actual); 

    } 

Collection.AreEquivilent,Collection.AreEqual和Collection.Contains全部失敗;但是,當我手動查看列表的輸出時,它們是相同的。

爲什麼NUnit不認識?

回答

2

如果測試失敗,你不會期望它,那麼我想你沒有正確實施Equals方法爲你的一些類(可能不是他們),因爲這是最終被調用時方法,當斷言對象集合。

如果方法沒有任何的IEntityRule實現然後object類的Equals方法被調用,而不是返回false實施時比較的對象是不相同的實例。

總結 - 解決方案是實現public bool Equals(AutomaticDataLossEntityRule other)AutomaticDataLossEntityRule類,類似地爲其餘的類。

相關問題