{"id":369,"date":"2018-12-16T16:40:00","date_gmt":"2018-12-16T03:40:00","guid":{"rendered":"https:\/\/blog.wiseowls.co.nz\/?p=369"},"modified":"2026-03-08T00:44:42","modified_gmt":"2026-03-07T11:44:42","slug":"integration-testing-aide-for-mvc-core-routing","status":"publish","type":"post","link":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/","title":{"rendered":"Integration testing aide for MVC core routing"},"content":{"rendered":"\n<p>Sometimes unit tests just don&#8217;t cut it. This is where integration tests come in. This however brings a whole new set of issues with finding the beast way to isolate the aspects under test and mock everything else away.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Problem statement<\/h2>\n\n\n\n<p>Suppose, we&#8217;ve got an api and a test that needs to make an http call to our api endpoint, like so:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"csharp\" class=\"language-csharp\"> [ApiController]\n  public class TestController : ControllerBase {\n\n    public IActionResult OkTest() {\n      return Ok(true);\n    }\n  }\n.....\npublic class TestControllerTests {\n\n    private readonly HttpClient _client;\n\n    public TestControllerTests() {\n      _client = TestSetup.GetTestClient();\n    }\n\n    [Test]\n    public async Task OkTest() {\n      var path = GetPathHere(nameof(OkTest)); \/\/ should return \"\/api\/test\/oktest\".\n      var response = await _client.GetAsync(path);\n      response.EnsureSuccessStatusCode();\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Solution<\/h2>\n\n\n\n<p>Knowing that ASP.NET Core comes with such a lightweight package now, and exposes so many extensibility points, one approach we found efficient was to build up the whole  <code>Host<\/code> and query its properties:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"csharp\" class=\"language-csharp\">private string GetPathHere(string actionName)\n    {\n        var host = Program.CreateWebHostBuilder(new string[] { }).Build();\n        host.Start();\n        IActionDescriptorCollectionProvider provider = (host.Services as ServiceProvider).GetService&lt;IActionDescriptorCollectionProvider>();\n        return provider.ActionDescriptors.Items.First(i => (i as ControllerActionDescriptor)?.ActionName == actionName).AttributeRouteInfo.Template;\n    }\n\n    [TestMethod]\n    public void OkTestShouldBeFine()\n    {\n        var path = GetPathHere(nameof(ValuesController.OkTest)); \/\/ \"api\/test\/oktest\"\n    }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Applicability <\/h2>\n\n\n\n<p>This is pretty basic case we&#8217;ve been dealing with, and the code makes quite a few assumptions. This approach however seems to hold up pretty well and surely will be our starting point next time round we test MVC actions!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes unit tests just don&#8217;t cut it. This is where integration tests come in. This however brings a whole new set of issues with finding the beast way to isolate the aspects under test and mock everything else away. Problem statement Suppose, we&#8217;ve got an api and a test that needs to make an http &hellip; <a href=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Integration testing aide for MVC core routing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":353,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[12],"class_list":["post-369","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Integration testing aide for MVC core routing - Timur and associates<\/title>\n<meta name=\"description\" content=\"Getting MVC Core route paths programmatically in integration tests so we don&#039;t have to hardcode URL strings.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Integration testing aide for MVC core routing - Timur and associates\" \/>\n<meta property=\"og:description\" content=\"Getting MVC Core route paths programmatically in integration tests so we don&#039;t have to hardcode URL strings.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/\" \/>\n<meta property=\"og:site_name\" content=\"Timur and associates\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-16T03:40:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-07T11:44:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2021\/01\/kevin-ku-w7ZyuGYNpRQ-unsplash-scaled.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"1919\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"timur\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@TimurKh\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/\"},\"author\":{\"name\":\"timur\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\"},\"headline\":\"Integration testing aide for MVC core routing\",\"datePublished\":\"2018-12-16T03:40:00+00:00\",\"dateModified\":\"2026-03-07T11:44:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/\"},\"wordCount\":144,\"image\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/kevin-ku-w7ZyuGYNpRQ-unsplash-scaled.jpg\",\"keywords\":[\"c#\"],\"articleSection\":[\"Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/\",\"url\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/\",\"name\":\"Integration testing aide for MVC core routing - Timur and associates\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/kevin-ku-w7ZyuGYNpRQ-unsplash-scaled.jpg\",\"datePublished\":\"2018-12-16T03:40:00+00:00\",\"dateModified\":\"2026-03-07T11:44:42+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\"},\"description\":\"Getting MVC Core route paths programmatically in integration tests so we don't have to hardcode URL strings.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/#primaryimage\",\"url\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/kevin-ku-w7ZyuGYNpRQ-unsplash-scaled.jpg\",\"contentUrl\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/kevin-ku-w7ZyuGYNpRQ-unsplash-scaled.jpg\",\"width\":2560,\"height\":1919,\"caption\":\"Photo by Kevin Ku on Unsplash\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/12\\\/16\\\/integration-testing-aide-for-mvc-core-routing\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Integration testing aide for MVC core routing\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#website\",\"url\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/\",\"name\":\"Timur and associates\",\"description\":\"Notes of an IT contractor\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\",\"name\":\"timur\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"caption\":\"timur\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/TimurKh\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Integration testing aide for MVC core routing - Timur and associates","description":"Getting MVC Core route paths programmatically in integration tests so we don't have to hardcode URL strings.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/","og_locale":"en_US","og_type":"article","og_title":"Integration testing aide for MVC core routing - Timur and associates","og_description":"Getting MVC Core route paths programmatically in integration tests so we don't have to hardcode URL strings.","og_url":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/","og_site_name":"Timur and associates","article_published_time":"2018-12-16T03:40:00+00:00","article_modified_time":"2026-03-07T11:44:42+00:00","og_image":[{"width":2560,"height":1919,"url":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2021\/01\/kevin-ku-w7ZyuGYNpRQ-unsplash-scaled.jpg","type":"image\/jpeg"}],"author":"timur","twitter_card":"summary_large_image","twitter_creator":"@TimurKh","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/#article","isPartOf":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/"},"author":{"name":"timur","@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59"},"headline":"Integration testing aide for MVC core routing","datePublished":"2018-12-16T03:40:00+00:00","dateModified":"2026-03-07T11:44:42+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/"},"wordCount":144,"image":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2021\/01\/kevin-ku-w7ZyuGYNpRQ-unsplash-scaled.jpg","keywords":["c#"],"articleSection":["Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/","url":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/","name":"Integration testing aide for MVC core routing - Timur and associates","isPartOf":{"@id":"https:\/\/blog.wiseowls.co.nz\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/#primaryimage"},"image":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2021\/01\/kevin-ku-w7ZyuGYNpRQ-unsplash-scaled.jpg","datePublished":"2018-12-16T03:40:00+00:00","dateModified":"2026-03-07T11:44:42+00:00","author":{"@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59"},"description":"Getting MVC Core route paths programmatically in integration tests so we don't have to hardcode URL strings.","breadcrumb":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/#primaryimage","url":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2021\/01\/kevin-ku-w7ZyuGYNpRQ-unsplash-scaled.jpg","contentUrl":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2021\/01\/kevin-ku-w7ZyuGYNpRQ-unsplash-scaled.jpg","width":2560,"height":1919,"caption":"Photo by Kevin Ku on Unsplash"},{"@type":"BreadcrumbList","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/12\/16\/integration-testing-aide-for-mvc-core-routing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.wiseowls.co.nz\/"},{"@type":"ListItem","position":2,"name":"Integration testing aide for MVC core routing"}]},{"@type":"WebSite","@id":"https:\/\/blog.wiseowls.co.nz\/#website","url":"https:\/\/blog.wiseowls.co.nz\/","name":"Timur and associates","description":"Notes of an IT contractor","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.wiseowls.co.nz\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59","name":"timur","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","caption":"timur"},"sameAs":["https:\/\/x.com\/TimurKh"]}]}},"_links":{"self":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/comments?post=369"}],"version-history":[{"count":2,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/369\/revisions"}],"predecessor-version":[{"id":371,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/369\/revisions\/371"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/media\/353"}],"wp:attachment":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/media?parent=369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/categories?post=369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/tags?post=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}