Unit testing Url.IsLocalUrl (returnUrl.ToString ()), how can I make it return false in unit test?

From the standard LogOn method in the account controller in MVC3 applications, how can I check

Url.IsLocalUrl(returnUrl.ToString()) 

line of code where the url is not local? In other words, which url should I use in this line of code during unit testing to make it return false?

I used the following consideration: this will be returned as false (not local):

 Uri uri = new Uri(@"http://www.google.com/blahblah.html"); 

But he just threw an exception in unit tests

Edit: I have to add that the LogOn method now looks like this:

 public ActionResult LogOn(LogOnModel model, System.Uri returnUrl) if (ModelState.IsValid) { bool loggedOn = LogOn(model); if (loggedOn) { if (Url.IsLocalUrl(returnUrl.ToString())) { return Redirect(returnUrl.ToString()); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } // If we got this far, something failed, redisplay form return View(viewModel); } 

Some errors of code / code code analysis forced the variable from string to System.uri, but it is very similar to the standard original.

To clarify, in unit test - I want to check and approve the result of hitting the Else line, where it is redirected to Home/Index , so I need to pass something to (System.Uri)returnUrl , which will make it return false to Url.IsLocalUrl and do not throw an exception

Further editing:

I use the MvcContrib testhelper, which pretty well mocks a lot of httpcontext and web content:

 Builder = new TestControllerBuilder(); UserController = new UserController(); Builder.InitializeController(UserController); 
+4
source share
1 answer

You need to make fun of the HttpContext as well as the UrlHelper instance on the controller that you are testing on the module. Here is an example of what unit test looks like if you use Moq :

 [TestMethod] public void LogOn_Should_Redirect_To_Home_If_Authentication_Succeeds_But_Not_Local_ReturnUrl_Is_Provided() { // arrange var sut = new AccountController(); var model = new LogOnModel(); var returnUrl = new Uri("http://www.google.com"); var httpContext = new Mock<HttpContextBase>(); var request = new Mock<HttpRequestBase>(); httpContext.Setup(x => x.Request).Returns(request.Object); request.Setup(x => x.Url).Returns(new Uri("http://localhost:123")); var requestContext = new RequestContext(httpContext.Object, new RouteData()); sut.Url = new UrlHelper(requestContext); // act var actual = sut.LogOn(model, returnUrl); // assert Assert.IsInstanceOfType(actual, typeof(RedirectToRouteResult)); var result = (RedirectToRouteResult)actual; Assert.AreEqual("Home", result.RouteValues["controller"]); Assert.AreEqual("Index", result.RouteValues["action"]); } 

Note. Since you have actually demonstrated the LogOn implementation that you invoke to verify credentials, you may need to adapt the unit test to ensure that this method returns true, first of all, given the model, to enter the if (loggedOn) .


UPDATE:

It seems you are using MvcContrib.TestHelper , which does all the HttpContext setup for you. So, all you have to do is mock the relevant parts for your unit test:

 [TestMethod] public void LogOn_Should_Redirect_To_Home_If_Authentication_Succeeds_But_Not_Local_ReturnUrl_Is_Provided() { // arrange var sut = new AccountController(); new TestControllerBuilder().InitializeController(sut); var model = new LogOnModel(); var returnUrl = new Uri("http://www.google.com"); sut.HttpContext.Request.Expect(x => x.Url).Return(new Uri("http://localhost:123")); // act var actual = sut.LogOn(model, returnUrl); // assert actual .AssertActionRedirect() .ToController("Home") .ToAction("Index"); } 

Usually the first 2 lines of unit test can be moved to the global [SetUp] method to avoid repeating them in each unit test for this controller, so now your test becomes a little clean:

 [TestMethod] public void LogOn_Should_Redirect_To_Home_If_Authentication_Succeeds_But_Not_Local_ReturnUrl_Is_Provided() { // arrange var model = new LogOnModel(); var returnUrl = new Uri("http://www.google.com"); _sut.HttpContext.Request.Expect(x => x.Url).Return(new Uri("http://localhost:123")); // act var actual = _sut.LogOn(model, returnUrl); // assert actual .AssertActionRedirect() .ToController("Home") .ToAction("Index"); } 
+15
source

Source: https://habr.com/ru/post/1398577/


All Articles