0

Possible Duplicate:
How to throw a SqlException(need for mocking)

I want to mock the throwing of SqlException when ExecuteNonQuery is executed;

System.Data.SqlClient.Moles.MSqlCommand.AllInstances.ExecuteNonQuery =
            (command) =>
                {
                    throw new MSqlException();
                };

This doesn't work as the compiler complains MSqlException doesn't derive from Exception. Am I going about this the wrong way, will I need to wrap ExecuteNonQuery in my code to achieve this?

Community
  • 1
  • 1
SeeNoWeevil
  • 2,549
  • 4
  • 26
  • 39
  • 1
    Related? http://stackoverflow.com/questions/1386962/how-to-throw-a-sqlexceptionneed-for-mocking – ChrisF Dec 24 '12 at 12:08
  • Yeah I'd just found that. The response here solves this, you basically need to cast it; http://stackoverflow.com/a/3795751/771698 – SeeNoWeevil Dec 24 '12 at 12:18

2 Answers2

0

If you were to throw a SqlException I would expect this to work. It inherits from DbException, which inherits from ExternalException, which inherits from ExternalException.

If you write a custom exception (I am assuming MSqlException is one), you should still inherit from an exception class...

public class MSqlException: SqlException {
Fenton
  • 241,084
  • 71
  • 387
  • 401
0

The answer was found in another question. The mole needs casting.

stackoverflow.com/a/3795751/771698

SeeNoWeevil
  • 2,549
  • 4
  • 26
  • 39