{"id":116947,"date":"2024-06-04T07:26:57","date_gmt":"2024-06-04T05:26:57","guid":{"rendered":"https:\/\/code-maze.com\/?p=116947"},"modified":"2024-06-04T07:29:58","modified_gmt":"2024-06-04T05:29:58","slug":"aspnetcore-mock-iconfiguration-getvalue","status":"publish","type":"post","link":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/","title":{"rendered":"How to Mock IConfiguration.GetValue in ASP.NET Core"},"content":{"rendered":"<p>In this article, we&#8217;ll take a closer look at how we can mock IConfiguration.GetValue when writing unit tests in ASP.NET Core.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/dotnet-testing\/HowToMockIConfiguration\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s start!<\/p>\n<h2>Mock IConfiguration.GetValue in ASP.NET Core<\/h2>\n<p>Before we start mocking, we need a class that utilizes <code>IConfiguration<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class FinanceService(IConfiguration configuration) : IFinanceService\r\n{\r\n    public double CalculateTotalAmount(double hours)\r\n    {\r\n        var hourlyRate = configuration.GetValue&lt;double&gt;(\"FinanceSettings:HourlyRate\");\r\n\r\n        return hourlyRate * hours;\r\n    }\r\n}<\/pre>\n<p>We create the <code>FinanceService<\/code> class and inject an <code>IConfiguration<\/code> instance using a <a href=\"https:\/\/code-maze.com\/csharp-primary-constructors-for-classes-and-structs\/\" target=\"_blank\" rel=\"noopener\">primary constructor<\/a>. Next, we create the <code>CalculateTotalAmount()<\/code> method that takes a <code>double<\/code> as a parameter, representing the hours worked. Then, inside the method, we use the <code>GetValue()<\/code> method from <code>IConfiguration<\/code> to get the hourly rate from our configuration file. Finally, we return the total amount.<\/p>\n<div style=\"padding: 20px; border-left: 5px gray solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">Do you want to know more about the different configuration providers in ASP.NET Core? Then you can check out our article <a href=\"https:\/\/code-maze.com\/aspnet-configuration-providers\/\" target=\"_blank\" rel=\"noopener\">ASP.NET Core Configuration \u2013 Configuration Providers<\/a>.<\/div>\n<p>Next, we&#8217;ll explore how we can mock the <code>GetValue()<\/code> method using two of the most popular mocking libraries &#8211; <code>Moq<\/code> and <code>NSubstitute<\/code>.<\/p>\n<h3>How to Mock IConfiguration.GetValue Using Moq<\/h3>\n<p>After we&#8217;ve created our test project, we create a new test class. After this is done, we can start writing our test:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"9-11,13-15\">[Theory]\r\n[InlineData(1)]\r\n[InlineData(1.5)]\r\n[InlineData(1000)]\r\npublic void WhenCalculateTotalAmountIsInvoked_ThenValidResultIsReturned(double hours)\r\n{\r\n    \/\/ Arrange\r\n    const string rate = \"25.50\";\r\n    var mockedSection = new Mock&lt;IConfigurationSection&gt;();\r\n    mockedSection.Setup(x =&gt; x.Value)\r\n        .Returns(rate);\r\n\r\n    var configuration = new Mock&lt;IConfiguration&gt;();\r\n    configuration.Setup(x =&gt; x.GetSection(\"FinanceSettings:HourlyRate\"))\r\n        .Returns(mockedSection.Object);\r\n\r\n    var financeService = new FinanceService(configuration.Object);\r\n\r\n    \/\/ Act\r\n    var result = financeService.CalculateTotalAmount(hours);\r\n\r\n    \/\/ Assert\r\n    result.Should().Be(hours * double.Parse(rate));\r\n}<\/pre>\n<p>We start by mocking an <code>IConfigurationSection<\/code> instance. Then we use the <code>Moq<\/code>&#8216;s <code>Setup()<\/code> method to select the <code>Value<\/code> property of the configuration section and then use the <code>Returns()<\/code> method to return a string representation of our hourly rate. <strong>This is a vital step that should not be missed as the <code>GetValue()<\/code> method ultimately calls the <code>GetSection()<\/code> method <\/strong><a href=\"https:\/\/github.com\/dotnet\/runtime\/blob\/9e6ba1f68c6a9c7206dacdf1e4cac67ea19931eb\/src\/libraries\/Microsoft.Extensions.Configuration.Binder\/src\/ConfigurationBinder.cs#L201\" target=\"_blank\" rel=\"nofollow noopener\"><strong>behind the scenes<\/strong><\/a>.<\/p>\n<p>Next, we continue with mocking <code>IConfiguration<\/code> itself. Also, we use the <code>Setup()<\/code> and <code>Returns()<\/code> methods, to specify that when we call the <code>GetSection()<\/code> method with <code>FinanceSettings:HourlyRate<\/code> as a parameter, it will return the already mocked <code>IConfigurationSection<\/code> instance.<\/p>\n<p>After this is done, we create a new instance of our <code>FinanceService<\/code> class by passing the <code>Object<\/code> property of the mocked <code>IConfiguration<\/code> instance and call the <code>CalculateTotalAmount()<\/code> method to get the result. Finally,\u00a0 we assert that it should be equal to the multiplication result of our hourly rate and the hours worked.<\/p>\n<h3>How to Mock IConfiguration.GetValue Using NSubstitute<\/h3>\n<p>Let&#8217;s create a new class and test method so we can utilize <code>NSubstitute<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"9-10,12-14\">[Theory]\r\n[InlineData(1)]\r\n[InlineData(1.5)]\r\n[InlineData(1000)]\r\npublic void WhenCalculateTotalAmountIsInvoked_ThenValidResultIsReturned(double hours)\r\n{\r\n    \/\/ Arrange\r\n    const string rate = \"25.50\";\r\n    var mockedSection = Substitute.For&lt;IConfigurationSection&gt;();\r\n    mockedSection.Value.Returns(rate);\r\n\r\n    var configuration = Substitute.For&lt;IConfiguration&gt;();\r\n    configuration.GetSection(\"FinanceSettings:HourlyRate\")\r\n        .Returns(mockedSection);\r\n\r\n    var financeService = new FinanceService(configuration);\r\n\r\n    \/\/ Act\r\n    var result = financeService.CalculateTotalAmount(hours);\r\n\r\n    \/\/ Assert\r\n    result.Should().Be(hours * double.Parse(rate));\r\n}<\/pre>\n<p>As we already know it&#8217;s mandatory to mock <code>IConfigurationSection<\/code>, we use <code>NSubstitute<\/code>&#8216;s <code>For&lt;T&gt;()<\/code> method to create one. Here, we don&#8217;t have a setup method, so we access the <code>Value<\/code> property directly and follow it with the <code>Returns()<\/code> method to which we pass our rate as a string.<\/p>\n<p>Next, we create a mock for <code>IConfiguration<\/code> and using the <code>Returns()<\/code> method again, specifying that when we call the <code>GetSection()<\/code> method with <code>FinanceSettings:HourlyRate<\/code> as a parameter, we will get our mocked configuration section.<\/p>\n<div style=\"padding: 20px; border-left: 5px gray solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">If you want to know more about NSubstitute, check out our article <a href=\"https:\/\/code-maze.com\/csharp-effective-mocking-with-nsubstitute\/\" target=\"_blank\" rel=\"noopener\">Effective Mocking With NSubstitute in .NET<\/a>.<\/div>\n<p>Then, in the rest of our test, we instantiate <code>FinanceService<\/code> class, call it&#8217;s <code>CalculateTotalAmount()<\/code> method and assert that it returns the expected result.<\/p>\n<h2>How to Create IConfiguration Instead of Mocking It in ASP.NET Core<\/h2>\n<p>Mocking <code>IConfiguration<\/code> with either <code>Moq<\/code> or <code>NSubstitute<\/code> does require a bit of effort as we need to mock <code>IConfigurationSection<\/code> as well. There is a way we can create a configuration instance without relying on external libraries.<\/p>\n<p>Let&#8217;s examine this approach:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"9-14\">[Theory]\r\n[InlineData(1)]\r\n[InlineData(1.5)]\r\n[InlineData(1000)]\r\npublic void WhenCalculateTotalAmountIsInvoked_ThenValidResultIsReturned(double hours)\r\n{\r\n    \/\/ Arrange\r\n    const string rate = \"25.50\";\r\n    var configuration = new ConfigurationBuilder()\r\n        .AddInMemoryCollection(new Dictionary&lt;string, string?&gt;\r\n        {\r\n            {\"FinanceSettings:HourlyRate\", rate}\r\n        })\r\n        .Build();\r\n\r\n    var financeService = new FinanceService(configuration);\r\n\r\n    \/\/ Act\r\n    var result = financeService.CalculateTotalAmount(hours);\r\n\r\n    \/\/ Assert\r\n    result.Should().Be(hours * double.Parse(rate));\r\n}<\/pre>\n<p>In a test method, we start by creating a new instance of the <code>ConfigurationBuilder<\/code> class. Then we use the <code>AddInMemoryCollection()<\/code> method that will add an in-memory collection to the configuration provider. The method requires an <code>IEnumerable&lt;KeyValuePair&lt;string, string?&gt;&gt;<\/code> instance as a parameter, to satisfy this condition we pass a <code>Dictionary&lt;string, string?&gt;<\/code> that has the <code>FinanceSettings:HourlyRate<\/code> as a key and the hourly rate as a value.<\/p>\n<div style=\"padding: 20px; border-left: 5px gray solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">The Options Pattern is a better alternative to using IConfiguration, you can check our article if you are not familiar with it <a href=\"https:\/\/code-maze.com\/aspnet-configuration-options\/\" target=\"_blank\" rel=\"noopener\">ASP.NET Core Configuration \u2013 Options Pattern<\/a>.<\/div>\n<p>Finally, we call the <code>Build()<\/code> method to instantiate an <code>IConfigurationRoot<\/code> instance. It implements the <code>IConfiguration<\/code> interface so we can safely use it to create an instance of our <code>FinanceService<\/code> class and test its <code>CalculateTotalAmount()<\/code> method.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we saw that mocking the IConfiguration.GetValue method is a vital skill when writing unit tests. We can leverage libraries like Moq and NSubstitute, to simulate the configuration environment, ensuring our tests are isolated and reliable. But we must be sure also to mock the configuration section as it is a dependency of the GetValue method. Alternatively, we explored how to build an in-memory configuration provider without relying on external providers. By mastering both approaches, we can ensure that our applications are robust and maintainable, with reliable unit tests that accurately simulate real-world configurations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;ll take a closer look at how we can mock IConfiguration.GetValue when writing unit tests in ASP.NET Core. Let&#8217;s start! Mock IConfiguration.GetValue in ASP.NET Core Before we start mocking, we need a class that utilizes IConfiguration: public class FinanceService(IConfiguration configuration) : IFinanceService { public double CalculateTotalAmount(double hours) { var hourlyRate = configuration.GetValue&lt;double&gt;(&#8220;FinanceSettings:HourlyRate&#8221;); [&hellip;]<\/p>\n","protected":false},"author":118,"featured_media":62187,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12,1112],"tags":[1811,1436,1337,583,1890,576],"class_list":["post-116947","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","category-testing","tag-c","tag-iconfiguration","tag-mocking","tag-moq","tag-nsubstitute","tag-testing","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Mock IConfiguration.GetValue in ASP.NET Core - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we&#039;ll take a closer look at how we can mock IConfiguration.GetValue when writing unit tests in ASP.NET Core.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Mock IConfiguration.GetValue in ASP.NET Core - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we&#039;ll take a closer look at how we can mock IConfiguration.GetValue when writing unit tests in ASP.NET Core.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-04T05:26:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-04T05:29:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ivan Gechev\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ivan Gechev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/\"},\"author\":{\"name\":\"Ivan Gechev\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/38034383b28698d52ffed31e7c7e5258\"},\"headline\":\"How to Mock IConfiguration.GetValue in ASP.NET Core\",\"datePublished\":\"2024-06-04T05:26:57+00:00\",\"dateModified\":\"2024-06-04T05:29:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/\"},\"wordCount\":743,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"keywords\":[\"C#\",\"IConfiguration\",\"Mocking\",\"moq\",\"NSubstitute\",\"Testing\"],\"articleSection\":[\"C#\",\"Testing\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/\",\"url\":\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/\",\"name\":\"How to Mock IConfiguration.GetValue in ASP.NET Core - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"datePublished\":\"2024-06-04T05:26:57+00:00\",\"dateModified\":\"2024-06-04T05:29:58+00:00\",\"description\":\"In this article, we'll take a closer look at how we can mock IConfiguration.GetValue when writing unit tests in ASP.NET Core.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"width\":1100,\"height\":620,\"caption\":\"ASP.NET Core\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Mock IConfiguration.GetValue in ASP.NET Core\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/38034383b28698d52ffed31e7c7e5258\",\"name\":\"Ivan Gechev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/02\/Ivan-Gechev-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/02\/Ivan-Gechev-150x150.png\",\"caption\":\"Ivan Gechev\"},\"description\":\"Ivan is a passionate developer currently working as a Business Management Systems Developer. He focuses on back-end development with C# and .NET, mainly targeting the Azure ecosystem and its various products and services.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/ivan-gechev-989753171\"],\"url\":\"https:\/\/code-maze.com\/author\/ibanov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Mock IConfiguration.GetValue in ASP.NET Core - Code Maze","description":"In this article, we'll take a closer look at how we can mock IConfiguration.GetValue when writing unit tests in ASP.NET Core.","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:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/","og_locale":"en_US","og_type":"article","og_title":"How to Mock IConfiguration.GetValue in ASP.NET Core - Code Maze","og_description":"In this article, we'll take a closer look at how we can mock IConfiguration.GetValue when writing unit tests in ASP.NET Core.","og_url":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/","og_site_name":"Code Maze","article_published_time":"2024-06-04T05:26:57+00:00","article_modified_time":"2024-06-04T05:29:58+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","type":"image\/png"}],"author":"Ivan Gechev","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Ivan Gechev","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/"},"author":{"name":"Ivan Gechev","@id":"https:\/\/code-maze.com\/#\/schema\/person\/38034383b28698d52ffed31e7c7e5258"},"headline":"How to Mock IConfiguration.GetValue in ASP.NET Core","datePublished":"2024-06-04T05:26:57+00:00","dateModified":"2024-06-04T05:29:58+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/"},"wordCount":743,"commentCount":2,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","keywords":["C#","IConfiguration","Mocking","moq","NSubstitute","Testing"],"articleSection":["C#","Testing"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/","url":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/","name":"How to Mock IConfiguration.GetValue in ASP.NET Core - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","datePublished":"2024-06-04T05:26:57+00:00","dateModified":"2024-06-04T05:29:58+00:00","description":"In this article, we'll take a closer look at how we can mock IConfiguration.GetValue when writing unit tests in ASP.NET Core.","breadcrumb":{"@id":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","width":1100,"height":620,"caption":"ASP.NET Core"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/aspnetcore-mock-iconfiguration-getvalue\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"How to Mock IConfiguration.GetValue in ASP.NET Core"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/38034383b28698d52ffed31e7c7e5258","name":"Ivan Gechev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/02\/Ivan-Gechev-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/02\/Ivan-Gechev-150x150.png","caption":"Ivan Gechev"},"description":"Ivan is a passionate developer currently working as a Business Management Systems Developer. He focuses on back-end development with C# and .NET, mainly targeting the Azure ecosystem and its various products and services.","sameAs":["https:\/\/www.linkedin.com\/in\/ivan-gechev-989753171"],"url":"https:\/\/code-maze.com\/author\/ibanov\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/116947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/118"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=116947"}],"version-history":[{"count":2,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/116947\/revisions"}],"predecessor-version":[{"id":117041,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/116947\/revisions\/117041"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62187"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=116947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=116947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=116947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}