chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi,

I am writing Under Test Code for testing the below method of my code

public int[] getsomething(string num)
{
 int[] numlist;
  numlist =getnumlist("",num);
  if (reversenumlist == true)
  {
    Array.Reverse(numlist);
  }
return (numlist);
}


My Unit Testing is like this
[TestMethod]
        public void getsomething_test()
        {

            int[] expected = { 1, 2, 3 };
            int[] actual;

            TOEVelocityColumn toeVelColFake = Isolate.Fake.Instance<TOEVelocityColumn>();
            Isolate.WhenCalled(() => toeVelColFake.getnumlist("", "1")).WillReturn(expected);
            Isolate.WhenCalled(() => toeVelColFake.getsomething("1")).CallOriginal();
            Isolate.WhenCalled(() => toeVelColFake.ReverseNozzleDirection).WillReturn(true);
          
            actual = toeVelColFake.getsomething("1");
            
            Assert.AreNotEqual(expected, actual);
     }


As you see, in the UTC, I am returning the expected variable on a method call.
In Act region, when I call the actual method, I am getting the reverse list but the expected variable also is getting reverse as a result the assert is failing because both expected and actual are getting reversed.
Can you please let me know how can I make sure that the expected variable will not get reverse?

James
asked by JamesKing (8.2k points)

1 Answer

0 votes
Hi,

The reason it happens is since the method Array.Reverse changes the state of the argument.
Since the passed argument is the one the test compares at the end, the assert fails.

The solution is to split the variable into two variables.
The first one is 'sampleArray' which will be the one used by WillReturn and the second one is 'expected' as the one you have today on which the assert will be.
answered by Shai Barak (1.5k points)
...