Skip to content

Instantly share code, notes, and snippets.

@miklund
Last active January 15, 2016 19:47
Show Gist options
  • Select an option

  • Save miklund/9d7b5efd8cee710baa55 to your computer and use it in GitHub Desktop.

Select an option

Save miklund/9d7b5efd8cee710baa55 to your computer and use it in GitHub Desktop.
2009-06-19 Mocking log4net calls
# Title: Mocking log4net calls
# Author: Mikael Lundin
# Link: http://blog.mikaellundin.name/2009/06/19/mocking-log4net-calls.html
public Customer RetrieveCustomer(Guid id)
{
Customer result = null;
try
{
result = dataAccess.GetCustomerByID(id);
}
catch (DbException ex)
{
Log.Error("An exception was thrown from the database", ex);
}
return result;
}
[TestMethod]
public void ShouldSwallowDbExceptionButLogError()
{
/* Setup */
var exception = new TestDbException();
var da = MockRepository.GenerateStub<DAL.CustomerDataAccess>();
ILog log = MockRepository.GenerateStub<ILog>();
var repository = new CustomerRepository(da, log);
/* Arrange */
da.Stub(dataAccess => dataAccess.GetCustomerByID(Arg<Guid>.Is.Anything))
.Throw(exception);
/* Act */
repository.RetrieveCustomer(Guid.Empty);
/* Assert */
log.AssertWasCalled(logger =>
logger.Error(Arg<string>.Is.Anything, Arg<Exception>.Is.Same(exception)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment