简体   繁体   English

运行单元测试时抛出异常

[英]Exception thrown when running unit test

I'm implementing the quickstart features of Moq as a learning exercise by creating a simple console application.我通过创建一个简单的控制台应用程序来实现Moq的快速入门功能作为学习练习。 When I run the app I see the exception.当我运行应用程序时,我看到了异常。

class Foo : IFoo
{
    public bool DoSomething(string value)
    {
        return false;
    }
}

public class Program
{
    private static Mock<IFoo> mock = new Mock<IFoo>();

    static void Main(string[] args)
    {
      mock.Setup(foo => foo.DoSomething("reset")).Throws<InvalidOperationException>();
      Assert.That(() => mock.Object.DoSomething("reset"), 
          Throws.InvalidOperationException);
    }
}

This does not look like a Moq problem, more a I can't catch the exception thrown in DoSomething problem .这看起来不像是Moq量问题,更像是I can't catch the exception throw in DoSomething problem I will assume that you use the nunit framework.我将假设您使用nunit框架。

Try to use the built in method Assert.Throws for exception assertion尝试使用内置方法 Assert.Throws进行异常断言

Assert.Throws<InvalidOperationException>(
      () => { mock.Object.DoSomething("reset"); });

It looks like if you run unit tests as part of an application and you are in debug mode you will see the exception being thrown as a popup dialog.看起来如果您将单元测试作为应用程序的一部分运行并且您处于调试模式,您将看到异常作为弹出对话框抛出。 Same as using the test running in debug mode.与使用在调试模式下运行的测试相同。 Running in release mode does not show the exception.在发布模式下运行不会显示异常。

暂无
暂无

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

相关问题 未设置断点时在单元测试期间引发异常 - Exception thrown during unit test when breakpoint is not set 在单元测试用例中捕获引发的异常的问题 - Issue in catching the thrown exception in Unit Test Case 从单元测试中调用调度程序时,传播在继续任务中引发的异常 - Propagate exception thrown in continuation task when invoking dispatcher from unit test 如何在单元测试抛出异常时获得100%的覆盖率? - How do I obtain 100% coverage when an exception is thrown in a unit test? 虽然抛出异常,但测试ExpectedException的单元测试失败 - Unit test that tests ExpectedException is failing though exception is thrown 单元测试如何确认已抛出异常 - How can a unit test confirm an exception has been thrown 调试单元测试时执行随机跳转抛出异常 - Execution Randomly Jumps to Thrown Exception while debugging Unit Test 未单独运行测试时,由Moq.Proxy.CastleProxyFactory的类型初始化器引发的异常 - Exception thrown by the type initializer for Moq.Proxy.CastleProxyFactory when not running test in isolation 在终结器中抛出异常时,Visual Studio 测试资源管理器未完成运行 NUnit 测试 - Visual Studio Test Explorer doesn't finish running NUnit tests when exception in finalizer is thrown 仅在从单元测试运行时进行映射时,在构造函数中分配IMappingEngine会导致映射异常 - Assigning IMappingEngine in constructor causes mapping exception when mapping only when running from unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM