Hi all,
I am just starting out with unit testing and have a scenario I'm not sure how to appraoch and my solution doesn't feel right.
I have a bit of code that does something, if it fails i.e. throws an exception the exception is caught and logged as below.
public T CreateTypedObjectInstance<T>()
{
T o = default(T);
try
{
o = Activator.CreateInstance<T>();
}
catch (Exception ex)
{
LogError(ex);
throw ex;
}
return o;
}
private void LogError(Exception ex)
{
if (logger != null)
{
logger.LogError(ex);
}
}
I want to test that if an error is thrown it calls the LogError() method which in turn calls another object.
I have approached this using a mock for the logger and catch the first exception thrown and then assert the LogError method was called. However, this doesn't feel right needing to catch an exception? I remeber read something that it is bad to have try catches in tests? Are there any other way to perform this test or should I refactor the logic? Any ideas would be great!
[Test]
public void CreateTypedObjectInstance_GivenTypeWithoutPrivateContructor_LogErrorToLogger()
{
//Setup Method used
MockRepository mockery = new MockRepository();
ILogger mockedLogger = mockery.StrictMock<ILogger>();
genericObjectFactoryInstance.Logger = mockedLogger;
Expect.Call( delegate { mockedLogger.LogError(null); } ).IgnoreArguments();
mockery.ReplayAll();
// this will throw an error as String does not have a parameterless constructor
try
{
genericObjectFactoryInstance.CreateTypedObjectInstance<String>();
}
catch { /*ignore this error to test behaviour after*/ }
mockery.VerifyAll();
}
EDIT
Using Mark Rushakoff answer the test becomes and works like a charm.
[Test]
public void CreateTypedObjectInstance_GivenTypeWithoutPrivateContructor_LogErrorToLogger()
{
//Setup Method used
MockRepository mockery = new MockRepository();
ILogger mockedLogger = mockery.StrictMock<ILogger>();
genericObjectFactoryInstance.Logger = mockedLogger;
Expect.Call( delegate { mockedLogger.LogError(null); } ).IgnoreArguments();
mockery.ReplayAll();
Assert.Throws<MissingMethodException>(() => genericObjectFactoryInstance.CreateTypedObjectInstance<String>());
mockery.VerifyAll();
}