c# - Assert.AreEqual fails on a class derived from List<> that overrides Equals() -
i've been having troubles nunit. have class inherits list , overrides equals() (so 2 instances can considered equal when contain same elements in different order). when using assert.areequal, fails, using assert.true , calling equals manually works :
[test] public void equals() { var dieset1 = new dieset {new die(1), new die(2)}; var dieset2 = new dieset {new die(2), new die(1)}; assert.true(dieset1.equals(dieset2)); //ok assert.areequal(dieset1, dieset2); //fails exception }
here exception detail :
nunit.framework.assertionexception unhandled user code hresult=-2146233088 message= expected , actual both 2 elements values differ @ index [0] expected: was:
source=nunit.framework stacktrace: @ nunit.framework.assert.that(object actual, iresolveconstraint expression, string message, object[] args) @ nunit.framework.assert.areequal(object expected, object actual) @ dicelibtest.diesettest.equals() in c:\dev_code\dicelib\dicelibtest\diesettest.cs:line 47 innerexception:
i set breakpoint in equals(), , made sure wasn't called when assert.areequal called.
my class :
public class dieset : list<die>, irollable { }
i've read related questions, instance : nunit doesn't work assert.areequal
but double checked , equals() method has correct signature.
public override bool equals(object obj) { }
my guess nunit internally has special behaviors on list<> and/or arrays and/or collections in general, , class falls in 1 of these categories. other guess i'm doing wrong , i'm failing see obvious..
yup - it's documented:
beginning version 2.4, multi-dimensioned arrays, nested arrays (arrays of arrays) , collections may compared. 2 arrays or collections treated equal assert.areequal if have same dimensions , if each of corresponding elements equal.
so want assert.true
version instead, basically.
for most users, behaviour desirable - happens conflict particular use.
personally wouldn't make set-semantics type derive list<t>
start though - if implemented iset<t>
instead (and used composition) may work.
Comments
Post a Comment