public async void RedirectToAction_Execute_PassesCorrectValuesToRedirect() { // Arrange var expectedUrl = "SampleAction"; var expectedPermanentFlag = false; var httpContext = new Mock<HttpContext>(); httpContext .SetupGet(o => o.RequestServices) .Returns(CreateServices().BuildServiceProvider()); var httpResponse = new Mock<HttpResponse>(); httpContext .Setup(o => o.Response) .Returns(httpResponse.Object); var actionContext = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor()); var urlHelper = GetMockUrlHelper(expectedUrl); var result = new RedirectToActionResult("SampleAction", null, null) { UrlHelper = urlHelper, }; // Act await result.ExecuteResultAsync(actionContext); // Assert // Verifying if Redirect was called with the specific Url and parameter flag. // Thus we verify that the Url returned by UrlHelper is passed properly to // Redirect method and that the method is called exactly once. httpResponse.Verify(r => r.Redirect(expectedUrl, expectedPermanentFlag), Times.Exactly(1)); }
public async Task RedirectToAction_Execute_ThrowsOnNullUrl() { // Arrange var httpContext = new Mock<HttpContext>(); httpContext .Setup(o => o.Response) .Returns(new Mock<HttpResponse>().Object); httpContext .SetupGet(o => o.RequestServices) .Returns(CreateServices().BuildServiceProvider()); var actionContext = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor()); var urlHelper = GetMockUrlHelper(returnValue: null); var result = new RedirectToActionResult(null, null, null) { UrlHelper = urlHelper, }; // Act & Assert await ExceptionAssert.ThrowsAsync<InvalidOperationException>( async () => { await result.ExecuteResultAsync(actionContext); }, "No route matches the supplied values."); }
public async Task RedirectToAction_Execute_ThrowsOnNullUrl() { // Arrange var httpContext = new Mock <HttpContext>(); httpContext.Setup(o => o.Response).Returns(new Mock <HttpResponse>().Object); var actionContext = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor()); var urlHelper = GetMockUrlHelper(returnValue: null); var result = new RedirectToActionResult(null, null, null) { UrlHelper = urlHelper, }; // Act & Assert await ExceptionAssert.ThrowsAsync <InvalidOperationException>( async() => { await result.ExecuteResultAsync(actionContext); }, "No route matches the supplied values."); }