[英]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.