Visual Studio与xUnit,Assert.Throws和“异常未被用户代码处理” [英] Visual studio with xUnit, Assert.Throws and "Exception was unhandled by user code"

查看:323
本文介绍了Visual Studio与xUnit,Assert.Throws和“异常未被用户代码处理”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xUnit 1.8.0.1549在dll应用程序(VS2010 / C#)中运行测试。
为了做到这一点,我通过visual studio使用项目属性中的启动操作下的启动外部程序运行xUnit,通过GUI运行程序运行dll(C:\mypath\xunit.gui.clr4)。 x86.exe)。



我想测试一些方法是否引发异常,为此,我使用以下内容:

  Assert.Throws< Exception>(
delegate
{
//我的方法来测试...
string tmp = p.TotalPayload;
}
);

问题是调试器在我的方法中停止,当异常被提出时,异常是未被用户代码处理。这是坏的,因为它阻止了gui赛跑者所有的时间,迫使我按F5。
我想顺利运行测试,我该怎么做?
谢谢

解决方案

当您检查异常是否发生时,您将必须处理单元测试中的异常码。现在,你没有这样做。



这里有一个例子:
我有一个读取文件名并进行一些处理的方法: p>

  public void ReadCurveFile(string curveFileName)
{
if(curveFileName == null)//为空
throw new ArgumentNullException(nameof(curveFileName));
if(!File.Exists(curveFileName))//不存在
throw new ArgumentException({0}不存在,curveFileName);

... etc
现在我写了一个测试方法来测试这样的代码:

  [事实] 
public void TestReadCurveFile()
{
MyClass tbGenTest = new MyClass ();

try
{
tbGenTest.ReadCurveFile(null);
}
catch(Exception ex)
{
Assert.True(ex为ArgumentNullException);
}
try
{
tbGenTest.ReadCurveFile(@TestData\PCMTestFile2.csv);
}
catch(Exception ex)
{
Assert.True(ex为ArgumentException);
}

现在您的测试应该通过!<​​/ p>

i am trying to run tests inside a dll application (VS2010/C#) using using xUnit 1.8.0.1549. To do so i run xUnit via visual studio using "Start External Program" under "Start Action" in the project properties, running the dll via the GUI runner (C:\mypath\xunit.gui.clr4.x86.exe).

I want to test if some methods raise exception, to do so, i use something like the following:

Assert.Throws<Exception>(
   delegate
   {
       //my method to test...
       string tmp = p.TotalPayload;
   }
);

The problem is that the debugger stops, inside my method, when the exception is raised saying "Exception was unhandled by user code". That's bad because it stops the gui runner all the time, forcing me to press F5. I would like to run the tests smoothly, how do I do this? Thanks

解决方案

When you check for whether the exception occurred, you will have to handle the exception in your unit test code. Right now, you aren't doing that.

Here's an example: I have a method that reads in a file name and does some processing:

  public void ReadCurveFile(string curveFileName)
    {           
        if (curveFileName == null) //is null
            throw new ArgumentNullException(nameof(curveFileName)); 
        if (!File.Exists(curveFileName))//doesn't exists
            throw new ArgumentException("{0} Does'nt exists", curveFileName);     

...etc Now I write a test method to test this code like this:

    [Fact]
    public void TestReadCurveFile()
    {
        MyClass tbGenTest = new MyClass ();

        try
        {
            tbGenTest.ReadCurveFile(null);
        }
        catch (Exception ex)
        {
            Assert.True(ex is ArgumentNullException);
        }
        try
        {
            tbGenTest.ReadCurveFile(@"TestData\PCMTestFile2.csv");
        }
        catch (Exception ex)
        {
            Assert.True(ex is ArgumentException);
        }

Now your test should pass !

这篇关于Visual Studio与xUnit,Assert.Throws和“异常未被用户代码处理”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆