2010-07-13 18 views
8

Esiste qualcosa simile a CollectionAssert.AreEquivalent() che funziona con le raccolte nidificate?Confronto collezione NUnit nidificato

Il codice seguente ...

CollectionAssert.AreEquivalent ( 
    new Dictionary<int, Dictionary<int, string>> 
    { 
     { 1, new Dictionary < int, string > { { 10, "foo" }, { 11, "bar" }, { 12, "spam" } } }, 
     { 2, new Dictionary < int, string > { { 20, "eggs" }, { 21, "eels" } } }, 
     { 3, new Dictionary < int, string > { { 30, "hovercraft" } } } 
    }, 
    new Dictionary<int, Dictionary<int, string>> 
    { 
     { 1, new Dictionary < int, string > { { 10, "foo" }, { 11, "bar" }, { 12, "spam" } } }, 
     { 2, new Dictionary < int, string > { { 20, "eggs" }, { 21, "eels" } } }, 
     { 3, new Dictionary < int, string > { { 30, "hovercraft" } } } 
    }); 

genera questa eccezione ...

Expected: equivalent to 
    < [1, System.Collections.Generic.Dictionary`2[System.Int32,System.String]], 
    [2, System.Collections.Generic.Dictionary`2[System.Int32,System.String]], 
    [3, System.Collections.Generic.Dictionary`2[System.Int32,System.String]] > 
But was: 
    < [1, System.Collections.Generic.Dictionary`2[System.Int32,System.String]], 
    [2, System.Collections.Generic.Dictionary`2[System.Int32,System.String]], 
    [3, System.Collections.Generic.Dictionary`2[System.Int32,System.String]] > 

La seguente asserzione passa:

CollectionAssert.AreEquivalent (
    new Dictionary < int, string > { { 10, "foo" }, { 11, "bar" }, { 12, "spam" } }, 
    new Dictionary < int, string > { { 10, "foo" }, { 11, "bar" }, { 12, "spam" } }); 

Se faccio le modifiche alla raccolta previsto , l'affermazione genera un'eccezione con l'intero contenuto di entrambe le raccolte nel messaggio:

Expected: equivalent to < [10, foo], [11, bar], [12, spam] > 
But was: < [10, foo], [11, bar], [12, eggs] > 

Sto usando NUnit 2.4.7.0.

+0

domanda simile: http://stackoverflow.com/questions/4185164/recursive-assert-on-collection –

risposta

0

Avresti bisogno di scrivere il tuo. Tuttavia, se fossi in me, proverei a scrivere i miei Assert in un modo diverso in modo che se ci fosse una differenza nei due elenchi fosse più ovvio perché/cosa era sbagliato.

+0

Il metodo esistente è abbastanza chiaro sui risultati attesi e reali e dove si trova il problema. Aggiornerò la mia domanda con quello che mi piacerebbe vedere come output. – MikeWyatt

0

una vecchia questione, ma qualcuno appena pubblicato un link ad esso su NUnit-discuss ...

Il fallimento è perché la versione di NUnit utilizzato non supportava confronti di uguaglianza tra due dizionari e ricadde sul confronto oggetto . Le versioni recenti non avranno questo problema.