How to throw a SqlException when needed for mocking and unit testing?

前端 未结 14 1691
孤街浪徒
孤街浪徒 2020-12-05 01:28

I am trying to test some exceptions in my project and one of the Exceptions I catch is SQlException.

It seems that you can\'t go new SqlException(

14条回答
  •  不思量自难忘°
    2020-12-05 02:21

    Theese solutions feel bloated.

    The ctor is internal, yes.

    (Without using reflection, the easiest way to just genuinely create this exception....

       instance.Setup(x => x.MyMethod())
                .Callback(() => new SqlConnection("Server=pleasethrow;Database=anexception;Connection Timeout=1").Open());
    

    Perphaps there's another method that doesn't require the timeout of 1 second to throw.

提交回复
热议问题