Search code examples
c#unit-testingmockingmoqnpgsql

C# NpgsqlException mock Moq


I want to mock some method to throw NpgsqlException, like this:

_someMock.Setup(x => x.SomeMethod(It.IsAny<int>())).Throws(new NpgsqlException());

But there is no public constructor for NpgsqlException, I tried to use reflection instead but it found no constructors for this exception(even private)


Solution

  • //Arrange
    var pgException = new PostgresException("messageText", "severity", "invariantSeverity", "sqlState");
            
    //Setup
    _someMock.Setup(x => x.SomeMethod(It.IsAny<int>())).ThrowsAsync(pgException);