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); }
public void Create_With_NullCommand_Returns_BadRequest() { BadRequestObjectResult result; using (PatronController controller = new PatronController(cmdQueue, dbContextFactory)) { result = controller.Create(null) as BadRequestObjectResult; } cmdQueueMock.Verify(c => c.Publish(It.IsAny <ICommand>()), Times.Never()); Assert.IsType <BadRequestObjectResult>(result); Assert.IsType <SerializableError>(result.Value); }
public void Create_With_ModelStateError_Returns_BadRequest() { BadRequestObjectResult result; using (PatronController controller = new PatronController(cmdQueue, dbContextFactory)) { controller.ModelState.AddModelError("DisplayName", "Display Name error is being tested."); result = controller.Create(new CreatePatron("Patron Web Test", TestHelper.Now)) as BadRequestObjectResult; } cmdQueueMock.Verify(c => c.Publish(It.IsAny <ICommand>()), Times.Never()); Assert.IsType <BadRequestObjectResult>(result); Assert.IsType <SerializableError>(result.Value); }