{"_id":"54cfccf73995cf0d0006f729","project":"54cfb736bba1a0230087419e","version":{"_id":"54cfb736bba1a023008741a1","__v":5,"project":"54cfb736bba1a0230087419e","createdAt":"2015-02-02T17:43:18.560Z","releaseDate":"2015-02-02T17:43:18.560Z","categories":["54cfb737bba1a023008741a2","54cfc6f75ff7e617002bbdae","54cfcbaebba1a023008741fc","54cfd0295ff7e617002bbdd9","54cfd05b3995cf0d0006f741"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"__v":8,"user":"54c4ca54686bce0d00efe1b9","category":{"_id":"54cfc6f75ff7e617002bbdae","__v":11,"project":"54cfb736bba1a0230087419e","pages":["54cfc714bba1a023008741f1","54cfcc24bba1a02300874200","54cfcc545ff7e617002bbdbe","54cfccf73995cf0d0006f729","54cfce14bba1a02300874205","54cfce42bba1a02300874209","54cfceef3995cf0d0006f736","54cfcf7dbba1a02300874218","54cfcfae3995cf0d0006f73b","54cfcfc4bba1a0230087421b","54cfcfdc3995cf0d0006f73d"],"version":"54cfb736bba1a023008741a1","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-02-02T18:50:31.446Z","from_sync":false,"order":2,"slug":"usage","title":"Usage"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-02-02T19:16:07.107Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Redirect to url\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"_controller.WithCallTo(c => c.Index()).ShouldRedirectTo(\\\"http://www.google.com.au/\\\");\\n\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Redirect to route name\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"_controller.WithCallTo(c => c.Index()).ShouldRedirectToRoute(\\\"RouteName\\\");\\n\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Redirect to action within the same controller\"\n}\n[/block]\nThere are a number of ways that you can specify this test, depending on the signature of the action you are redirecting to.\n\nIf you are redirecting to an action that takes no parameters, or takes a single int parameter, then you can use a method group, which is the tersest specification (you don't need to specify the parameters to the action), e.g. if these are two actions in the controller you are testing:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public ActionResult ActionWithNoParameters()\\n{\\n    return new EmptyResult();\\n}\\npublic ActionResult RedirectToActionWithNoParameters()\\n{\\n    return RedirectToAction(\\\"ActionWithNoParameters\\\");\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThen you can write this test:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"_controller.WithCallTo(c => c.RedirectToActionWithNoParameters())\\n    .ShouldRedirectTo(c => c.ActionWithNoParameters);\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nWe can explicitly define whatever signatures we want to allow this terser syntax, but the different permutations that are possible are infinite and not possible for any custom types in your project. If anyone has ideas for how to do this better let us know!\n\nIf you have 1-3 parameters being passed into the action being redirected to then you can still use the method group, but you need to specify the types being passed into the action, e.g. if you have the following controller action being redirected to:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public ActionResult SomeAction(string param1, int param2, bool param3) {}\\n\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThen you can write the following test for the redirect:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"_controller.WithCallTo(c => c.Index())\\n    .ShouldRedirectTo<string, int, bool>(c => c.SomeAction);\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nIf you have more than three parameters, or you are uncomfortable with that syntax then you can specify a lambda with a call to the action you want and pass in dummy values for the parameters, e.g. for the previous example:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"_controller.WithCallTo(c => c.Index())\\n    .ShouldRedirectTo(c => c.SomeAction(null, 0, false));\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nYou can also pass through a `MethodInfo` object against the method you are redirecting to, e.g.:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"_controller.WithCallTo(c => c.Index())\\n    .ShouldRedirectTo(typeof(HomeController).GetMethod(\\\"SomeAction\\\"));\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nWe don't recommend using this latter option because it uses a \"magic\" string so if you change the action name then the string won't change and the test will break and also need to be updated. In saying that, if you change your parameters more often than the action name this might be a better option. If you do use this option be careful that you don't get an AmbiguousMatchException if there are multiple actions with the same name.\n\nAt this stage there isn't support for the [ActionName()] attribute or simply passing through a string to check against the action name, but if either are important to you feel free to [add an issue in the GitHub project.](https://github.com/TestStack/TestStack.FluentMVCTesting/issues)\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Redirect to action in another controller\"\n}\n[/block]\nIf you are redirecting to an action in another controller, then there are two syntaxes that you can currently use (similar to the last two mentioned above):\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"controller.WithCallTo(c => c.Index())\\n    .ShouldRedirectTo<SomeOtherController>(c2 => c2.SomeAction());\\n\\n_controller.WithCallTo(c => c.Index())\\n    .ShouldRedirectTo<SomeOtherController>(typeof(SomeOtherController).GetMethod(\\\"SomeAction\\\"));\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"redirects","type":"basic","title":"Redirects"}
[block:api-header] { "type": "basic", "title": "Redirect to url" } [/block] [block:code] { "codes": [ { "code": "_controller.WithCallTo(c => c.Index()).ShouldRedirectTo(\"http://www.google.com.au/\");\n", "language": "csharp" } ] } [/block] [block:api-header] { "type": "basic", "title": "Redirect to route name" } [/block] [block:code] { "codes": [ { "code": "_controller.WithCallTo(c => c.Index()).ShouldRedirectToRoute(\"RouteName\");\n", "language": "csharp" } ] } [/block] [block:api-header] { "type": "basic", "title": "Redirect to action within the same controller" } [/block] There are a number of ways that you can specify this test, depending on the signature of the action you are redirecting to. If you are redirecting to an action that takes no parameters, or takes a single int parameter, then you can use a method group, which is the tersest specification (you don't need to specify the parameters to the action), e.g. if these are two actions in the controller you are testing: [block:code] { "codes": [ { "code": "public ActionResult ActionWithNoParameters()\n{\n return new EmptyResult();\n}\npublic ActionResult RedirectToActionWithNoParameters()\n{\n return RedirectToAction(\"ActionWithNoParameters\");\n}", "language": "csharp" } ] } [/block] Then you can write this test: [block:code] { "codes": [ { "code": "_controller.WithCallTo(c => c.RedirectToActionWithNoParameters())\n .ShouldRedirectTo(c => c.ActionWithNoParameters);", "language": "csharp" } ] } [/block] We can explicitly define whatever signatures we want to allow this terser syntax, but the different permutations that are possible are infinite and not possible for any custom types in your project. If anyone has ideas for how to do this better let us know! If you have 1-3 parameters being passed into the action being redirected to then you can still use the method group, but you need to specify the types being passed into the action, e.g. if you have the following controller action being redirected to: [block:code] { "codes": [ { "code": "public ActionResult SomeAction(string param1, int param2, bool param3) {}\n", "language": "csharp" } ] } [/block] Then you can write the following test for the redirect: [block:code] { "codes": [ { "code": "_controller.WithCallTo(c => c.Index())\n .ShouldRedirectTo<string, int, bool>(c => c.SomeAction);", "language": "csharp" } ] } [/block] If you have more than three parameters, or you are uncomfortable with that syntax then you can specify a lambda with a call to the action you want and pass in dummy values for the parameters, e.g. for the previous example: [block:code] { "codes": [ { "code": "_controller.WithCallTo(c => c.Index())\n .ShouldRedirectTo(c => c.SomeAction(null, 0, false));", "language": "csharp" } ] } [/block] You can also pass through a `MethodInfo` object against the method you are redirecting to, e.g.: [block:code] { "codes": [ { "code": "_controller.WithCallTo(c => c.Index())\n .ShouldRedirectTo(typeof(HomeController).GetMethod(\"SomeAction\"));", "language": "csharp" } ] } [/block] We don't recommend using this latter option because it uses a "magic" string so if you change the action name then the string won't change and the test will break and also need to be updated. In saying that, if you change your parameters more often than the action name this might be a better option. If you do use this option be careful that you don't get an AmbiguousMatchException if there are multiple actions with the same name. At this stage there isn't support for the [ActionName()] attribute or simply passing through a string to check against the action name, but if either are important to you feel free to [add an issue in the GitHub project.](https://github.com/TestStack/TestStack.FluentMVCTesting/issues) [block:api-header] { "type": "basic", "title": "Redirect to action in another controller" } [/block] If you are redirecting to an action in another controller, then there are two syntaxes that you can currently use (similar to the last two mentioned above): [block:code] { "codes": [ { "code": "controller.WithCallTo(c => c.Index())\n .ShouldRedirectTo<SomeOtherController>(c2 => c2.SomeAction());\n\n_controller.WithCallTo(c => c.Index())\n .ShouldRedirectTo<SomeOtherController>(typeof(SomeOtherController).GetMethod(\"SomeAction\"));", "language": "csharp" } ] } [/block]