Пример #1
0
        public void Create_With_ValidCommand_IsPublished_AndReturnsAcceptedResult_WithLocation()
        {
            CreatePatron cmd = new CreatePatron("Patron Web Test", TestHelper.Now)
            {
                DisplayName = "Patron Display Name",
                IsAnonymous = false,
                PatronType  = "Test Patron"
            };

            cmdQueueMock.Setup(c => c.Publish(It.IsAny <CreatePatron>())).Verifiable();

            var mockUrlHelper = new Mock <IUrlHelper>(MockBehavior.Strict);
            Expression <Func <IUrlHelper, string> > urlSetup = url => url.Action(It.Is <UrlActionContext>(uac => uac.Action == "Get" && GetId(uac.Values) != cmd.Id));

            mockUrlHelper.Setup(urlSetup).Returns("a/mock/url/for/testing").Verifiable();

            AcceptedResult result;

            using (PatronController controller = new PatronController(cmdQueue, dbContextFactory)
            {
                Url = mockUrlHelper.Object
            })
            {
                result = controller.Create(cmd) as AcceptedResult;
            }

            Assert.IsType <AcceptedResult>(result);
            mockUrlHelper.Verify(urlSetup, Times.Once());
            cmdQueueMock.Verify(cq => cq.Publish(It.IsAny <CreatePatron>()), Times.Once());
            Assert.Equal("a/mock/url/for/testing", result.Location);
        }
Пример #2
0
        public void User_Can_Create_New_Patron()
        {
            CreatePatron command = new CreatePatron("Create User", TestHelper.Now)
            {
                DisplayName = "Test Patron",
                IsAnonymous = false,
                PatronType  = "Test Account"
            };

            var           eventBus       = new Mock <IEventBus>();
            PatronCreated publishedEvent = null;

            eventBus.Setup(bus => bus.Publish(It.IsAny <PatronCreated>()))
            .Callback <PatronCreated>(evnt => publishedEvent = evnt).Verifiable();

            ICommandHandler <CreatePatron> handler = new PatronCommandHandler(eventBus.Object);

            handler.Handle(command);

            Assert.NotEqual(Guid.Empty, command.Id);
            eventBus.VerifyPublish <PatronCreated>(Times.Once());
            Assert.NotNull(publishedEvent);
            Assert.Equal(command.DisplayName, publishedEvent.DisplayName);
            Assert.Equal(command.IsAnonymous, publishedEvent.IsAnonymous);
            Assert.Equal(command.PatronType, publishedEvent.PatronType);
            Assert.Equal(command.Id, publishedEvent.SourceId);
            Assert.Equal(command.PatronId, publishedEvent.PatronId);
            Assert.NotEqual(command.Id, publishedEvent.Id);
            Assert.NotEqual(command.PatronId, publishedEvent.Id);
            Assert.Equal(0, publishedEvent.Version);
        }
Пример #3
0
 public IActionResult Create([FromBody] CreatePatron command)
 {
     if (command == null || !ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     else
     {
         CommandQueue.Publish(command);
         return(Accepted(Url.Action(nameof(Get), new { id = command.PatronId })));
     }
 }