views:

333

answers:

6

I have a simple method that returns an array of the letters of the alphabet.

public char[] GetLettersOfTheAlphabet() {
    char[] result = new char[26];
    int index = 0;

    for (int i = 65; i < 91; i++) {
        result[index] = Convert.ToChar(i);
        index += 1;
    }

    return result;
}

I tried to unit test the code with

[TestMethod()]
public void GetLettersOfTheAlphabetTest_Pass() {
    HomePage.Rules.BrokerAccess.TridionCategories target = new HomePage.Rules.BrokerAccess.TridionCategories();
    char[] expected = new char[] { char.Parse("A"), 
                                   char.Parse("B"),
                                   char.Parse("C"),
                                   char.Parse("D"),
                                   char.Parse("E"),
                                   char.Parse("F"),
                                   char.Parse("G"),
                                   char.Parse("H"),
                                   char.Parse("I"),
                                   char.Parse("J"),
                                   char.Parse("k"),
                                   char.Parse("L"),
                                   char.Parse("M"),
                                   char.Parse("N"),
                                   char.Parse("O"),
                                   char.Parse("P"),
                                   char.Parse("Q"),
                                   char.Parse("R"),
                                   char.Parse("S"),
                                   char.Parse("T"),
                                   char.Parse("U"),
                                   char.Parse("V"),
                                   char.Parse("W"),
                                   char.Parse("X"),
                                   char.Parse("Y"),
                                   char.Parse("Z")};
    char[] actual;
    actual = target.GetLettersOfTheAlphabet();
    Assert.AreEqual(expected, actual);
}

It seems that it comes down the compare function that the array uses. How do you handle this case?

+2  A: 

Just iterate through the array and compare each item. Index of 4 in each array should be identical.

Another way is to check the result array if it contains say 'A', 'G', 'K' or random letters.

Something I have learned from Unit Tests sometimes you have to either copy code or do more work because that is the nature of testing. Basically what works for a unit test might not be acceptable for a production site.

David Basarab
A: 

Your K is lowercase in the expected array, but I believe that code generates uppercase characters. If the comparison is case sensitive, it would cause the failure when the two arrays don't match.

Brandon
A: 

You could also do a test on the size and a few spot checks of values in the array....

Assert.AreEqual(expected.Length, actual.Length)

Assert.AreEqual(expected[0], actual[0]) ....

klabranche
A: 

First of all:


Assert.AreEqual(expected.Length,actual.Length);

Then:


for(int i = 0; i < actual.Length; i++)
{

//Since you are testing whether the alphabet is correct, then is ok that the characters
//have different case.
  Assert.AreEqual(true,actual[i].toString().Equals(expected[i].toString(),StringComparison.OrdinalIgnoreCase));
}
Freddy
+3  A: 

If you're using .NET 3.5, you can also use SequenceEqual():

[TestMethod()]
public void GetLettersOfTheAlphabetTest_Pass() {
    var target = new HomePage.Rules.BrokerAccess.TridionCategories();
    var expected = new[] { 'A','B','C', ... };

    var actual = target.GetLettersOfTheAlphabet();
    Assert.IsTrue(expected.SequenceEqual(actual));
}
dahlbyk
A: 

CollectionAssert.AreEquivalent(expected, actual) works.

I am not exactly sure how it works but it does work for my needs. I think it checks each item in each item in the expected collection exsits in the actual column, it also checks that they are the same count, but order does not matter.

JustEngland
From the documentation on AreEquivalent: "Two collections are equivalent if they have the same elements in the same quantity, but in any order. Elements are equal if their values are equal, not if they refer to the same object."
strout