How to throw a SqlException(need for mocking)
Posted
by chobo2
on Stack Overflow
See other posts from Stack Overflow
or by chobo2
Published on 2009-09-06T23:11:42Z
Indexed on
2010/03/09
2:06 UTC
Read the original article
Hit count: 841
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");
© Stack Overflow or respective owner