Esempio n. 1
0
        public async Task Handler_sends_no_command_when_order_already_exists()
        {
            // Arrange
            var fakeGuid     = Guid.NewGuid();
            var fakeOrderCmd = new IdentifiedCommand <CreateOrderCommand, bool>(FakeOrderRequest(), fakeGuid);

            _requestManager.Setup(x => x.ExistAsync(It.IsAny <Guid>()))
            .Returns(Task.FromResult(true));

            _mediator.Setup(x => x.Send(It.IsAny <IRequest <bool> >(), default(System.Threading.CancellationToken)))
            .Returns(Task.FromResult(true));

            //Act
            var handler = new IdentifierCommandHandler <CreateOrderCommand, bool>(_mediator.Object, _requestManager.Object);
            var result  = await handler.Handle(fakeOrderCmd);

            //Assert
            Assert.False(result);
            _mediator.Verify(x => x.Send(It.IsAny <IRequest <bool> >(), default(System.Threading.CancellationToken)), Times.Never());
        }
        public async Task Handler_sends_command_when_order_no_exists()
        {
            // Arrange
            var fakeGuid     = Guid.NewGuid();
            var fakeOrderCmd = new IdentifiedCommand <CreateOrderCommand, bool>(FakeOrderRequest(), fakeGuid);

            _requestManager.Setup(x => x.ExistAsync(It.IsAny <Guid>()))
            .Returns(Task.FromResult(false));

            _mediator.Setup(x => x.SendAsync(It.IsAny <IAsyncRequest <bool> >()))
            .Returns(Task.FromResult(true));

            //Act
            var handler = new IdentifierCommandHandler <CreateOrderCommand, bool>(_mediator.Object, _requestManager.Object);
            var result  = await handler.Handle(fakeOrderCmd);

            //Assert
            Assert.True(result);
            _mediator.Verify(x => x.SendAsync(It.IsAny <IAsyncRequest <bool> >()), Times.Once());
        }
Esempio n. 3
0
        public async Task Handler_sends_no_command_when_order_exists()
        {
            //构造命令
            var fakeGuid     = Guid.NewGuid();
            var fakeOrderCmd = new IdentifiedCommand <CreateOrderCommand, bool>(this.FakeOrderRequest(), fakeGuid);

            //构造命令处理
            //模拟逻辑:订单存在,不处理命令(保证命令消费的幂等性)。
            this._requestManager
            .Setup(x => x.ExistAsync(It.IsAny <Guid>()))
            .Returns(Task.FromResult(true));
            this._mediator
            .Setup(x => x.Send(It.IsAny <IRequest <bool> >(), CancellationToken.None))
            .Returns(Task.FromResult(true));
            var handler = new IdentifierCommandHandler <CreateOrderCommand, bool>(this._mediator.Object, this._requestManager.Object);

            //执行命令
            var result = await handler.Handle(fakeOrderCmd);

            Assert.False(result);
            this._mediator.Verify(m => m.Send(It.IsAny <IRequest <bool> >(), CancellationToken.None), Times.Never());
        }