Richard
Richard

Reputation: 155

Exception thrown when running unit test

I'm implementing the quickstart features of Moq as a learning exercise by creating a simple console application. 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);
    }
}

Upvotes: 0

Views: 706

Answers (2)

Richard
Richard

Reputation: 155

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.

Upvotes: 0

Athanasios Kataras
Athanasios Kataras

Reputation: 26450

This does not look like a Moq problem, more a I can't catch the exception thrown in DoSomething problem. I will assume that you use the nunit framework.

Try to use the built in method Assert.Throws for exception assertion

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

Upvotes: 1

Related Questions