How to throw a SqlException(need for mocking)
- by chobo2
Hi
I am trying to test some exceptions in my project and one of the Exceptions I catch is SQlException.
Now It seems that you can't go new SqlException() so I am not sure how I can throw a exception especially without somehow calling the database(and since it is unit tests it is usually advised not to call the database since it is slow).
So I am using nunit and moq so I am not sure how to fake this.
Edit
Based on the answers they seem to all be based on ado.net I am using linq to sql. So that stuff is like behind the scenes.
Edit @ Matt Hamilton
System.ArgumentException : Type to mock must be an interface or an abstract or non-sealed class.
at Moq.Mock`1.CheckParameters()
at Moq.Mock`1..ctor(MockBehavior behavior, Object[] args)
at Moq.Mock`1..ctor(MockBehavior behavior)
at Moq.Mock`1..ctor()
Posts to the first line when it tries to mockup
var ex = new Mock<System.Data.SqlClient.SqlException>();
ex.SetupGet(e => e.Message).Returns("Exception message");