无法在 console.writeline 上打印测试方法

Cannot print on console.writeline on test methods

有谁知道为什么这永远不会打印出来?我可能忽略了一些简单的事情,但由于某种原因我无法在测试方法中打印任何内容,这和 console.write 似乎都不起作用。

有没有办法在测试方法中打印任何东西?

[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());

        } 

    }

您的代码有多个问题。

  • 您没有为数组分配任何值。
  • 您创建了一个包含 1 个元素的数组,并且您正在尝试使用该数组的第二个元素
  • 您使用了错误的大小写条件。

运行这个

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) 
    {

    }
}

你的 switch case 将永远不会执行,因为它们是负的,而如果条件为真,你就上层。并为 resul[1]

赋值

所以编辑为:

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

编辑:Harshit Shrivastava 说得对,您已声明长度为 1 的数组,但您正在为第二个索引分配值。所以用 resul[0]

替换 resul[1]