简体   繁体   English

有人可以告诉我如何在C#上修复此错误

[英]Can someone tell me how to fix this error on C#

I am a beginner to C# and I am getting this error Argument 1: Cannot convert from 'string' to 'System.Action' . 我是C#的初学者,但遇到此错误Argument 1: Cannot convert from 'string' to 'System.Action' I honestly do not know how to fix this. 老实说,我不知道该如何解决。 if you could help me please tell me what I should add to my code to fix this error. 如果您可以帮助我,请告诉我应该添加到代码中的内容以解决此错误。 I am basically writing a Unit Test that will test if the code is working correctly (the code is supposed to throw an exception). 我基本上是在编写一个单元测试,它将测试代码是否正常工作(代码应该抛出异常)。 This is my code: 这是我的代码:

Unit Test Project File: 单元测试项目文件:

[TestMethod]
    public void ExceptionTest()
    {
        var test = new JumpingCode();
        Assert.ThrowsException<ArgumentException>(test.EmptyValue(null));
    }

The Class File: 类文件:

namespace Jumping
{
    public class JumpingCode
    {
      public string EmptyValue(string some)
      {
          if (string.IsNullOrEmpty(some))
          {
            throw new ArgumentException("Name Cannot be null or empty");
          }

          return "Hello World";
      }

   }
}

I am basically testing a random throw. 我基本上是在测试随机抛出。 The Unit Test file is the one throwing the error messages. 单元测试文件是引发错误消息的文件。 The error messages are on this line Assert.ThrowsException<ArgumentException>(test.EmptyValue(null)) the test.EmptyValue(null) is the text that is underlined. 错误消息在此行上Assert.ThrowsException<ArgumentException>(test.EmptyValue(null)) test.EmptyValue(null)是带下划线的文本。 I honestly have no idea what is the problem. 老实说,我不知道这是什么问题。 I know I am doing something wrong but I just don't know what. 我知道我做错了事,但我只是不知道该怎么办。 I don't know if this will change anything but I have never wrote a unit test for an exception before. 我不知道这是否会改变任何东西,但是我之前从未编写过用于异常的单元测试。

You're passing the result of EmptyValue (a string ) to Assert.ThrowsException but this method expects an Action to be validated. 您正在将EmptyValuestring )的结果传递给Assert.ThrowsException但是此方法希望Action能够得到验证。

You have to change this 你必须改变这个

Assert.ThrowsException<ArgumentException>(test.EmptyValue(null));

by this 这样

Assert.ThrowsException<ArgumentException>(() => test.EmptyValue(null));

Assert.Throws returns the exception that's thrown which lets you assert on the exception Assert.Throws返回所引发的异常,使您可以断言该异常

So in case no exception is thrown, or an exception of the wrong type is thrown, the first Assert Throws assertion will fail. 因此,如果未引发任何异常或引发了错误类型的异常,则第一个Assert Throws断言将失败。

BUT if an exception of the correct type is thrown then you can now assert on the actual exception that you've saved in the variable. 但是,如果抛出了正确类型的异常,那么您现在可以对保存在变量中的实际异常进行断言。

You have to write it like this 你必须这样写

var ex = Assert.Throws<ArgumentNullException>(() => foo.Bar(null));

So in your case 所以你的情况

var ext = Assert.ThrowsException<ArgumentException>(() =>test.EmptyValue(null));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM