-1

Does anyone know why this never prints? I may be ignoring something simple but for some reason I cannot print anything within a test method, not this nor console.write seems to work.

Is there any way to print anything within test methods if any?

[TestMethod]
public void TestMethodAddUser()
    {

        int []  resul = new int[1]; 

        resultado = gestor.addUser("El Pelucas", "12345", "elpelucassabe@gmail.com");
        Console.WriteLine(resul[1].ToString());

        try
        {

            if (resul[1] > 0)
            {

                switch (resul[1])
                {

                    case -1:

                        Console.WriteLine("Username taken.");

                        break;

                    case -2:

                        Console.WriteLine("Email address taken.");

                        break;

                }

                Console.WriteLine("User added.");
                Assert.IsTrue(true);


            }

        }
        catch (Exception ex)
        {

            Assert.Fail(ex.ToString());

        } 

    }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Code Grasshopper
  • 610
  • 1
  • 11
  • 29
  • You should consider using TestContext or Trace.Xxxx methods instead to log during tests... In general you'd put messages as second/third argument of `Assert.Xxxx ` methods so you know what is wrong and verify assumptions... – Alexei Levenkov Aug 27 '15 at 04:00
  • @alexeilevenkov I'll look into it, thanks – Code Grasshopper Aug 27 '15 at 07:26

2 Answers2

1

Your switch case will never execute because they are negative and you upper if condition true for positive. And also assign value to resul[1]

So edit it as:

int input;
if (!int.TryParse(Console.ReadLine(), out input);
{
    Console.WriteLine("Invalid number");
}
else 
{
    resul[1] = input;
}    
if (resul[1] > 0) //use resul[1]<1 for negative switch case
{
     switch (resul[1])
     {
         case 1:    
            Console.WriteLine("Username taken.");    
            break;    
         case 2:    
            Console.WriteLine("Email address taken.");    
            break;    
     }    
     Console.WriteLine("User added.");
     Assert.IsTrue(true);
}

EDIT: Harshit Shrivastava saying right that you have declare array on length 1 but you are assigning the value at second index. So replace resul[1] with resul[0]

NASSER
  • 5,900
  • 7
  • 38
  • 57
0

Your code has multiple problems.

  • You are not assigning any value to your array.
  • You have created an array of 1 element & you are trying to use 2nd element of the array
  • You are using wrong case condition.

Run this

public void TestMethodAddUser() 
{
    int[] resul = new int[1];
    resul[0] = 1;

    Console.WriteLine(resul[0].ToString());

    try 
    {
        if (resul[0] > 0) 
        {
            switch (resul[0]) 
            {
                case 1:
                    Console.WriteLine("Username taken.");
                    break;

                case 2:
                    Console.WriteLine("Email address taken.");
                    break;
            }

            Console.WriteLine("User added.");
        }

    }
    catch (Exception ex) 
    {

    }
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
Harshit
  • 5,147
  • 9
  • 46
  • 93