How to compare 2 Arrays to Assert.Equals?

Asked

Viewed 609 times

4

I need to compare two arrays on Assert.Equals.

When I test the method in race it is right but the test does not pass.

Code:

public void SeparaStringTest() {
 RecebeComando recebecomando = new RecebeComando();
 string[] StringSeparada = {
  "A",
  "A",
  "B"
 };

 Assert.Equals(recebecomando.SeparaString("AAB"), StringSeparada);
 Assert.Equals(recebecomando.SeparaString("A A B"), StringSeparada);
 Assert.Equals(recebecomando.SeparaString("A-A-B"), StringSeparada);
}

public string[] SeparaString(string palavra) {
 palavra = palavra.Replace("-", "");
 palavra = palavra.Replace(" ", "");

 var comando = new string[palavra.Length];

 for (int i = 0; i < comando.Length; i++) {
  comando[i] = palavra[i].ToString();
 }
 return comando;
}

3 answers

6

The command Assert.AreEqual compares as a reference, so that you can compare arrays or lists, you must use the CollectionAssert.AreEqual

public void SeparaStringTest()
{
    RecebeComando recebecomando = new RecebeComando();
    string[] StringSeparada = { "A", "A", "B" };

    CollectionAssert.AreEqual(recebecomando.SeparaString("AAB"), StringSeparada);
    CollectionAssert.AreEqual(recebecomando.SeparaString("A A B"), StringSeparada);
    CollectionAssert.AreEqual(recebecomando.SeparaString("A-A-B"), StringSeparada);
}

See documentation from Microsoft:

Assert.Areequal: https://msdn.microsoft.com/pt-br/library/microsoft.visualstudio.testtools.unittesting.assert.areequal.aspx

Collectionassert.Areequal: https://msdn.microsoft.com/pt-br/library/ms243763.aspx

  • @In your question you are also using the wrong method in comparison (as stated by Jbueno) is not Assert.Equals, but Assert.Areequals, more than anything, Collectionassert should be used for collections.

5

First there’s a mistake there, Assert.Equals has the same behaviour as the method Equals of object, that is, this method checks whether two variables point to the same reference.

You probably want to use the method Assert.AreEqual, this will compare item by item of arrays and check their values.

I also took advantage and made a modification to leave the method SeparaString a little simpler.

public static string[] SeparaString(string palavra, char? separador)
{
    string[] comando;

    if(separador == null)
        comando = palavra.ToCharArray().Select(x => x.ToString()).ToArray();
    else
        comando = palavra.Split((char)separador);

    return comando;
}

The use would be

SeparaString("AAB", null);
SeparaString("AAB", '-');
SeparaString("AAB", ' ');

0

I managed to solve, in fact I had to change to Assert.Areequal the test was like this:

public void SeparaStringTest()
{
    RecebeComando recebecomando = new RecebeComando();           
    string[] retornoDoMétodo = recebecomando.SeparaString("AAB");
    Assert.AreEqual(retornoDoMétodo.Length, 3);
    Assert.AreEqual(retornoDoMétodo[0], "A");
    Assert.AreEqual(retornoDoMétodo[1], "A");
    Assert.AreEqual(retornoDoMétodo[2], "B");
}
  • 2

    Instead of creating an answer, you can comment on the answer that helped you the most and mark it as correct. It’s a way of thanking the person who wrote the answer and also letting the record show that the one who answered you the most.

  • This implementation you made is very plastered and not very productive, Collectionassert.Areequal does the dirty work for you and works with any Collection, that is, if you are going to compare another Array with more items, you do not need to include anything else in the code. I will edit my reply including an example of what you should implement.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.