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."); } }
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);