keeg
keeg

Reputation: 3978

unit testing an unexcpected exception?

Is it possible to mock or unit test for an unexpected exception? Code coverage is complaining that the catch is not being tested for, but how do you test it?

    public List<Blog> SelectActiveBlogs()
    {
        List<Blog> returnCode = null;

        try
        {
            returnCode = GetQueryable<Blog>().Where(b => b.IsActive).ToList();
        }
        catch (Exception ex)
        {
            ExceptionHandler.HandleException(ex);
        }

        return returnCode;
    }

EDIT: The GetQueryable is a method that uses a repository factory to get a db result, so there could be exceptions bubbling up from db interactions in production, so I want to make sure we catch them if something goes wrong an thus the catch block is present.

EDIT2: The exception is actually handled by a custom class to preserve the stack trace, once logged it's not re thrown. Question still remains as to whether I can mock and throw an exception in this case.

Upvotes: 0

Views: 1385

Answers (4)

saintedlama
saintedlama

Reputation: 6898

Don't mock the method GetQueryable() but the class that is is responsible to get the queryable.

I guess you have some code like this in GetQueryable()

private IQueryable<T> GetQueryable<T>()
{
     return repository.query...
}

So don't try to mock the method but the repository instance and throw an exception in the mock when query is called.

Mocking that using MoQ should not be to complicated - something like

var mock = new Moq.Mock<IRepository<Blog>>();
mock.Setup(r => r.Query()).Throws(new Exception("I'm really unexpected"));

I'm in doubt if this test will lead to a greater code quality or less bugs. But the above method will please the coverage tool ;)

Upvotes: 1

AD.Net
AD.Net

Reputation: 13399

If you want to get into the exception, you can mock the call GetQueryable<Blog>() and return NULL, that should make the code throw exception. Or you can just throw an exception in mocking that call, depends on how you implement the mock. BTW, you will lose the stack trace in that code if you do throw ex.

Upvotes: 0

dknaack
dknaack

Reputation: 60506

You can handle that using the ExpectedException Attribute, even if its unexcepeted. Its depend on the typeof parameter that you pass to the argument.

[TestMethod]
[ExpectedException(typeof(Exception),
"A exception has been throws.")]
public List<Blog> SelectActiveBlogs()
{
    List<Blog> returnCode = null;

    try
    {
        returnCode = GetQueryable<Blog>().Where(b => b.IsActive).ToList();
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return returnCode;
}

Another thing, if you implement it this way you dont need a try catch block. If you catch an exception and only rethrow it (without any other exception handling) it makes no sense.

Hope that helps, if not please leave a comment. Have a nice day!

Upvotes: 1

Joshua
Joshua

Reputation: 43317

I'd presume you don't test for it.

I've left behind many exception handlers for cases that can't be reached in test systems before.

In this particular case, you should just remove the try / catch / throw because that's nonsense code anyway.

Upvotes: 1

Related Questions