views:

83

answers:

2

Hello,

I have a test method:

    [TestMethod()]
    public void test_chars()
    {
        MyBO target = new MyBO() { x = 'S' };
        char[] expected = {'D','d','M','m','L','l'};
        char actual = target.s;
        Assert.AreEqual(actual, expected); // ?
    }

How can i check with Assert.AreEqual if target.x is in that char[] expected? So 'S' isn't part of that array, so test should fail. Is it possible?

Thanks

+4  A: 
Assert.IsTrue( ((IList)expected).Contains(actual));
James Curran
Array.Contains does not exist. What do u mean?
qwerty
Sorry about that. Wrote before I checked the syntax. It's fixed now.
James Curran
+5  A: 

Personally, I like the following:

Assert.IsTrue(expected.Any(x => x == actual));

This can be customized based on whatever type of comparison you need.

Mike
Assert.IsTrue(expected.Any(x => x == actual)); does not work.. there is no 'Any' method for expected, which is a char[] .
qwerty
+1 definitely nicer than mine (I hate having to cast)
James Curran
What release of .Net are you using? This should work in .Net 3.5 and newer. This is an extension method on Array.
Mike
I'm using .NET 3.5
qwerty
Make sure you have using System.Linq; at the top of your code file.
Mike