コード例 #1
0
        public async Task ExecuteAsync_transitions_based_on_StateFuncAsync()
        {
            var sut = new StateTransitionDynamicParameterizedAsync <Sale, SaleState, SaleEvent, string>(
                stateAccessor: sale => sale.State
                , stateMutator: (sale, newState) => sale.State = newState
                , stateFuncAsync: (sale, stringParam, cancelToken) => Task.FromResult(SaleState.Complete)
                , name: "test"
                , priority: 1);

            using (var cancellationSource = new CancellationTokenSource())
            {
                var sale = new Sale(saleID: 87)
                {
                    State = SaleState.Open
                };
                var parameters = new ExecutionParameters <Sale, SaleEvent>(SaleEvent.Pay, sale, cancellationSource.Token, "request");

                var result = await sut.ExecuteAsync(parameters);

                Assert.Equal(SaleState.Complete, sale.State);
                Assert.Equal(SaleState.Complete, result.CurrentState);
                Assert.Equal(SaleState.Open, result.PreviousState);
                Assert.False(result.WasCancelled);
            }
        }
コード例 #2
0
        public async Task ExecuteAsync_doesnt_send_notification_if_no_state_change()
        {
            var sut = new StateTransitionDynamicParameterizedAsync <Sale, SaleState, SaleEvent, string>(
                stateAccessor: sale => sale.State
                , stateMutator: (sale, newState) => sale.State = newState
                , stateFuncAsync: (sale, stringParam, cancelToken) => Task.FromResult(SaleState.Open)
                , name: "test"
                , priority: 1);
            var notificationReceived = false;

            StateTransitionDynamicAsync <Sale, SaleState, SaleEvent> .OnTransitionedEvent += (sale, _) => notificationReceived = true;

            using (var cancellationSource = new CancellationTokenSource())
            {
                var sale = new Sale(saleID: 87)
                {
                    State = SaleState.Open
                };
                var parameters = new ExecutionParameters <Sale, SaleEvent>(SaleEvent.Pay, sale, cancellationSource.Token, "request");

                await sut.ExecuteAsync(parameters);

                Assert.False(notificationReceived);
            }
        }
コード例 #3
0
        public async Task ExecuteAsync_throws_ArgumentException_if_parameter_wrong_type()
        {
            var sut = new StateTransitionDynamicParameterizedAsync <Sale, SaleState, SaleEvent, string>(
                stateAccessor: sale => sale.State
                , stateMutator: (sale, newState) => sale.State = newState
                , stateFuncAsync: (sale, stringParam, cancelToken) => Task.FromResult(SaleState.Complete)
                , name: "test"
                , priority: 1);

            var testSale = new Sale(saleID: 87)
            {
                State = SaleState.Open
            };
            var parameters = new ExecutionParameters <Sale, SaleEvent>(SaleEvent.Pay, testSale, request: 1);

            await Assert.ThrowsAsync <ArgumentException>(async() => await sut.ExecuteAsync(parameters));
        }