Esempio n. 1
0
        public void UnitOfWorkFilter_DbConcurrencyException()
        {
            UnitOfWorkAttribute attribute = new UnitOfWorkAttribute();

            var modelState = new ModelStateDictionary();

            var actionContext = new ActionContext(
                new Mock <HttpContext>().Object,
                new Mock <RouteData>().Object,
                new Mock <ActionDescriptor>().Object,
                modelState
                );

            var parameters = new Dictionary <string, object>();

            parameters["item"] = "not null";

            var transactionCoordinatorMock = new Mock <ITransactionCoordinator>();
            var updateEntry = new Mock <IUpdateEntry>();

            transactionCoordinatorMock.Setup(x => x.CommitTransaction()).Throws(new DbUpdateConcurrencyException("error", new List <IUpdateEntry>()
            {
                updateEntry.Object
            }));

            var dbcontextMock = new Mock <DbContext>();

            ApiHealthController controller = new ApiHealthController(new Mock <ApiSettings>().Object, new Mock <ILogger <ApiHealthController> >().Object, transactionCoordinatorMock.Object, dbcontextMock.Object);

            var actionExecutingContext = new ActionExecutingContext(
                actionContext,
                new List <IFilterMetadata>(),
                parameters,
                controller
                );

            var actionExecutedContext = new ActionExecutedContext(
                actionContext,
                new List <IFilterMetadata>(),
                controller
                );

            attribute.OnActionExecuting(actionExecutingContext);
            transactionCoordinatorMock.Verify(x => x.BeginTransaction());

            Action execute = () =>
            {
                attribute.OnActionExecuted(actionExecutedContext);
            };

            execute.Should().Throw <DbUpdateConcurrencyException>();
        }
Esempio n. 2
0
        public void UnitOfWorkFilter_Rollback()
        {
            UnitOfWorkAttribute attribute = new UnitOfWorkAttribute();

            var modelState = new ModelStateDictionary();

            var actionContext = new ActionContext(
                new Mock <HttpContext>().Object,
                new Mock <RouteData>().Object,
                new Mock <ActionDescriptor>().Object,
                modelState
                );

            var parameters = new Dictionary <string, object>();

            parameters["item"] = "not null";

            var transactionCoordinatorMock = new Mock <ITransactionCoordinator>();
            var updateEntry = new Mock <IUpdateEntry>();

            transactionCoordinatorMock.Setup(x => x.RollbackTransaction());

            var dbcontextMock = new Mock <DbContext>();

            ApiHealthController controller = new ApiHealthController(new Mock <ApiSettings>().Object, new Mock <ILogger <ApiHealthController> >().Object, transactionCoordinatorMock.Object, dbcontextMock.Object);

            var actionExecutingContext = new ActionExecutingContext(
                actionContext,
                new List <IFilterMetadata>(),
                parameters,
                controller
                );

            var actionExecutedContext = new ActionExecutedContext(
                actionContext,
                new List <IFilterMetadata>(),
                controller
                );

            actionExecutedContext.Exception = new Exception();

            attribute.OnActionExecuting(actionExecutingContext);
            transactionCoordinatorMock.Verify(x => x.BeginTransaction());
            attribute.OnActionExecuted(actionExecutedContext);

            transactionCoordinatorMock.Verify(x => x.RollbackTransaction());
        }