Esempio n. 1
0
        public void Test_Should_Commit_if_there_is_no_exception()
        {
            var uowAttr = new UnitOfWorkAttribute();
            var mockUoW = new Mock <IUnitOfWork>();

            mockUoW.Setup(u => u.Commit()).Verifiable();

            uowAttr.UnitOfWork = mockUoW.Object;
            uowAttr.OnActionExecuted(new ActionExecutedContext());

            mockUoW.Verify();
        }
Esempio n. 2
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. 3
0
        public void Test_Should_not_commit_if_there_is_Exception()
        {
            var uowAttr = new UnitOfWorkAttribute();
            var mockUoW = new Mock <IUnitOfWork>();

            mockUoW.Setup(u => u.Commit()).Throws <InvalidOperationException>();

            uowAttr.UnitOfWork = mockUoW.Object;
            uowAttr.OnActionExecuted(new ActionExecutedContext()
            {
                Exception = new Exception()
            });
        }
Esempio n. 4
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());
        }