Last active
January 15, 2016 19:47
-
-
Save miklund/9d7b5efd8cee710baa55 to your computer and use it in GitHub Desktop.
2009-06-19 Mocking log4net calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Title: Mocking log4net calls | |
| # Author: Mikael Lundin | |
| # Link: http://blog.mikaellundin.name/2009/06/19/mocking-log4net-calls.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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