Moq and throwing a SqlException

前端 未结 4 727
庸人自扰
庸人自扰 2020-12-25 11:25

I have the following code to test that when a certain name is passed to my method, it throws a SQL exception (there is reason to that one, although it sounds a little odd).<

4条回答
  •  感情败类
    2020-12-25 11:30

    I just tried this out, and it worked for me:

    private static void ThrowSqlException()
    {
        using (var cxn = new SqlConnection("Connection Timeout=1"))
        {
            cxn.Open();
        }
    }
    
    // ...
    mockAccountDAL.Setup(m => m.CreateAccount(It.IsAny),
                         "Display Name 2", It.IsAny()))
                  .Callback(() => ThrowSqlException());
    

提交回复
热议问题