Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ArgumentValidator.VerifyNotNullOrEmpty进行单元测试_C#_Unit Testing - Fatal编程技术网

C# 使用ArgumentValidator.VerifyNotNullOrEmpty进行单元测试

C# 使用ArgumentValidator.VerifyNotNullOrEmpty进行单元测试,c#,unit-testing,C#,Unit Testing,我正在做单元测试。我有以下方法: public PortfolioFileGetListDto(string documentsCaseNumber) { ArgumentValidator.VerifyNotNullOrEmpty(documentsCaseNumber, nameof(documentsCaseNumber)); CaseNumber = documentsCaseNumber; } 我试

我正在做单元测试。我有以下方法:

  public PortfolioFileGetListDto(string documentsCaseNumber)
        {
            ArgumentValidator.VerifyNotNullOrEmpty(documentsCaseNumber, nameof(documentsCaseNumber));

            CaseNumber = documentsCaseNumber;
        }
我试着这样对这个方法进行单元测试:

 [TestMethod]
        public void PortfolioFileGetListDto_Constructor_ShouldValidateParameters()
        {
            // Portfolio           
            Action act = () => new PortfolioFileGetListDto(null);
            act.Should().Throw<ArgumentNullException>().WithMessage("Value cannot be null.\n\rParameter name: portfolio");
        }
那么如何通过这个单元测试呢


谢谢

断言取决于您使用的单元测试框架

对于Microsoft TestPlatform:

var exception = Assert.ThrowsException<Exception>(x => {
    new PortfolioFileGetListDto(null);
});
Assert.AreEqual("Value cannot be null.\n\rParameter name: portfolio", exception.Message);
var exception=Assert.ThrowsException(x=>{
新PortfoliofileGetListTo(空);
});
Assert.AreEqual(“值不能为空。\n\r参数名称:公文包”,exception.Message);

断言取决于您使用的单元测试框架

对于Microsoft TestPlatform:

var exception = Assert.ThrowsException<Exception>(x => {
    new PortfolioFileGetListDto(null);
});
Assert.AreEqual("Value cannot be null.\n\rParameter name: portfolio", exception.Message);
var exception=Assert.ThrowsException(x=>{
新PortfoliofileGetListTo(空);
});
Assert.AreEqual(“值不能为空。\n\r参数名称:公文包”,exception.Message);

断言需要带有“.Parameter name:portfolio”的消息,但测试中的方法将引发参数名为“documentsCaseNumber”的异常

您真的想对异常消息严格要求吗?
我建议只检查变量的正确名称。已使用泛型断言确认的异常类型

[TestMethod]
public void Constructor_WithInvalidParameter_ThrowsException()
{     
    Action construct = () => new PortfolioFileGetListDto(null);
    construct.Should().Throw<ArgumentNullException>()
            .Which.Message.Should().ContainEquivalentOf("documentsCaseNumber");
}
[TestMethod]
带有InvalidParameter的公共无效构造函数\u ThrowsException()
{     
Action construct=()=>new portfolioFileGetListTo(null);
construct.Should().Throw()
.Which.Message.Should().ContainEquivalentOf(“documentsCaseNumber”);
}

断言需要带有“.Parameter name:portfolio”的消息,但测试中的方法将引发参数名为“documentsCaseNumber”的异常

您真的想对异常消息严格要求吗?
我建议只检查变量的正确名称。已使用泛型断言确认的异常类型

[TestMethod]
public void Constructor_WithInvalidParameter_ThrowsException()
{     
    Action construct = () => new PortfolioFileGetListDto(null);
    construct.Should().Throw<ArgumentNullException>()
            .Which.Message.Should().ContainEquivalentOf("documentsCaseNumber");
}
[TestMethod]
带有InvalidParameter的公共无效构造函数\u ThrowsException()
{     
Action construct=()=>new portfolioFileGetListTo(null);
construct.Should().Throw()
.Which.Message.Should().ContainEquivalentOf(“documentsCaseNumber”);
}

您是否在未调试的情况下运行了测试?是的,我这样做了,但失败了您是否在未调试的情况下运行了测试?是的,我这样做了,但失败了谢谢。但我有,那不是。但是,使用act.should()。还是没有?而且Assert也不会有一点悸动,除了你。但我有,那不是。但是,使用act.should()。或者不?断言没有ThrowsException