JustEngland
JustEngland

Reputation: 1390

Unit Test Collection

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?

Upvotes: 0

Views: 6110

Answers (6)

JustEngland
JustEngland

Reputation: 1390

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.

Upvotes: 4

Freddy
Freddy

Reputation: 3294

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));
}

Upvotes: 0

dahlbyk
dahlbyk

Reputation: 77620

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));
}

Upvotes: 5

Brandon
Brandon

Reputation: 70032

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.

Upvotes: 0

Kevin LaBranche
Kevin LaBranche

Reputation: 21098

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]) ....

Upvotes: 0

David Basarab
David Basarab

Reputation: 73351

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.

Upvotes: 2

Related Questions