Skip to content

Instantly share code, notes, and snippets.

@mgroves
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save mgroves/fdcc82ab252a56662a43 to your computer and use it in GitHub Desktop.

Select an option

Save mgroves/fdcc82ab252a56662a43 to your computer and use it in GitHub Desktop.
// while I'm not actually relying on
// PostSharp or any of my real service classes anywhere in my unit tests
// I still need a service class to stand in and receive errors
// that I can test assertions against
public class FakeService : ServiceBase
{
public List<string> Errors { get; private set; }
public FakeService()
{
Errors = new List<string>();
this.AddValidationError = x => Errors.Add(x);
}
}
public class When_invoking_a_service_method_with_sql_exception
{
static ServiceErrorConcern _concern;
static IMethodConcernArgs _mockArgs;
static FakeService _fakeService;
static SqlException _sqlException;
static IExceptionLogger _mockLogger;
Establish context = () =>
{
_mockLogger = Mock.Create<IExceptionLogger>();
_concern = new ServiceErrorConcern(_mockLogger);
_mockArgs = Mock.Create<IMethodConcernArgs>();
_fakeService = new FakeService();
Mock.Arrange(() => _mockArgs.Instance).Returns(_fakeService);
_sqlException = SqlExceptionHelper.CreateException();
Mock.Arrange(() => _mockArgs.Proceed()).Throws(_sqlException);
};
Because of = () =>
{
_concern.OnInvoke(_mockArgs);
};
It should_callback_with_a_single_constraint_error_message = () =>
_fakeService.Errors.Count.ShouldEqual(1);
It should_callback_with_an_appropriate_error_message = () =>
_fakeService.Errors[0].ShouldContain("There was a database error. Please try again later.");
It should_log_the_exception = () =>
Mock.Assert(() => _mockLogger.Log(Arg.Is(_sqlException)), Occurs.Once());
}
public class When_invoking_a_service_method_with_sql_constraint_exception
{
static ServiceErrorConcern _concern;
static IMethodConcernArgs _mockArgs;
static FakeService _fakeService;
static IExceptionLogger _mockLogger;
static SqlException _sqlException;
Establish context = () =>
{
_mockLogger = Mock.Create<IExceptionLogger>();
_concern = new ServiceErrorConcern(_mockLogger);
_mockArgs = Mock.Create<IMethodConcernArgs>();
_fakeService = new FakeService();
Mock.Arrange(() => _mockArgs.Instance).Returns(_fakeService);
_sqlException = SqlExceptionHelper.CreateException("xxx The DELETE statement conflicted with the REFERENCE constraint xxx");
Mock.Arrange(() => _mockArgs.Proceed()).Throws(_sqlException);
};
Because of = () =>
{
_concern.OnInvoke(_mockArgs);
};
It should_callback_with_a_single_constraint_error_message = () =>
_fakeService.Errors.Count.ShouldEqual(1);
It should_callback_with_an_appropriate_error_message = () =>
_fakeService.Errors[0].ShouldEqual("This item can't be deleted because it is still used by other items.");
It should_not_log_the_exception = () =>
Mock.Assert(() => _mockLogger.Log(Arg.IsAny<Exception>()), Occurs.Never());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment