<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Test Automation - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/test-automation/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Sun, 12 Nov 2023 17:49:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>Test Automation - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Top 10 Benefits of Unit Testing &#8211; Awesome Code</title>
		<link>https://thecodebuzz.com/top-10-benefits-of-unit-testing-awesome-code/</link>
					<comments>https://thecodebuzz.com/top-10-benefits-of-unit-testing-awesome-code/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 02 Jul 2023 04:08:02 +0000</pubDate>
				<category><![CDATA[Automation Testing]]></category>
		<category><![CDATA[Integration Testing]]></category>
		<category><![CDATA[Test Automation]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[10 Benefits of Unit Testing - Awesome Code]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=27779</guid>

					<description><![CDATA[<p>Top 10 Benefits of Unit Testing &#8211; Awesome Code Today, we&#8217;ll discuss the top 10 Benefits of Unit Testing procedures in this article. Unit testing is a powerful method that has become well-known. Software is broken down into its most basic functional units during unit testing, and these components are then put through a rigorous [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/top-10-benefits-of-unit-testing-awesome-code/">Top 10 Benefits of Unit Testing – Awesome Code</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">Top 10 Benefits of Unit Testing &#8211; Awesome Code</h1>



<p>Today, we&#8217;ll discuss the top 10 Benefits of Unit Testing procedures in this article. </p>



<p></p>



<p>Unit testing is a powerful method that has become well-known.</p>



<p></p>



<p>Software is broken down into its most basic functional units during unit testing, and these components are then put through a rigorous testing process. </p>



<p></p>



<p>The many advantages of unit testing will be discussed in this article, along with how it enhances the quality of software development as a whole.</p>



<p></p>



<p>We will see below benefits,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-improved-code-quality-benefits-of-unit-testing">Improved Code Quality  &#8211; Benefits of Unit Testing</a></li><li><a href="#aioseo-early-defect-bug-detection">Early Defect/Bug Detection</a></li><li><a href="#aioseo-facilitates-refactoring-and-code-maintenance">Facilitates Refactoring and Code Maintenance</a></li><li><a href="#aioseo-promotes-teamwork-and-collaboration">Promotes Teamwork and Collaboration</a></li><li><a href="#aioseo-unit-test-as-code-documentation">Unit Test as Code Documentation</a></li><li><a href="#aioseo-create-mock-behavior-describe-and-test-unexpected-behavior">Create Mock behavior &#8211; Describe and test Unexpected behavior</a></li><li><a href="#aioseo-controlled-test-scenarios-execution">Controlled Test Scenarios execution</a></li><li><a href="#aioseo-faster-debugging-and-troubleshooting">Faster Debugging and Troubleshooting</a></li><li><a href="#aioseo-test-driven-development-tdd">Test-Driven Development (TDD)</a></li><li><a href="#aioseo-summary">Summary</a></li></ul></div>



<p></p>



<h2 class="wp-block-heading" id="aioseo-improved-code-quality-benefits-of-unit-testing">Improved Code Quality  &#8211; Benefits of Unit Testing</h2>



<p></p>



<p>Unit testing helps developers identify and eliminate bugs at an early stage of development.</p>



<p>By writing tests that target individual units of code, such as functions or methods, developers can ensure that each unit behaves correctly in isolation. </p>



<p>This systematic approach to testing promotes clean and modular code design, leading to improved code quality. </p>



<p>With each unit passing its tests, developers gain confidence in the correctness of their codebase, reducing the likelihood of introducing bugs during subsequent development phases.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-early-defect-bug-detection">Early Defect/Bug Detection </h2>



<p></p>



<p><a href="https://www.thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/" target="_blank" rel="noopener" title="Unit Test Naming Conventions Best Practices">Unit testing </a>facilitates the detection of bugs at an early stage, allowing developers to address them promptly. </p>



<p></p>



<p>By isolating and testing individual units of code, issues can be identified before they propagate into complex and interconnected parts of the system. </p>



<p></p>



<p>This early bug detection significantly reduces the debugging effort required in later stages of development and enhances the overall efficiency of the software development process.</p>



<p></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-facilitates-refactoring-and-code-maintenance">Facilitates Refactoring and Code Maintenance </h2>



<p></p>



<p> Unit testing plays a vital role in code refactoring and maintenance. </p>



<p></p>



<p>Refactoring involves restructuring code without changing its external behavior. </p>



<p></p>



<p>Unit tests provide a safety net during refactoring, ensuring that the desired functionality is maintained after modifications. </p>



<p></p>



<p>By running unit tests after each refactoring step, developers can quickly identify any unintended side effects or regression issues. </p>



<p></p>



<p>This encourages developers to refactor code fearlessly, leading to cleaner, more maintainable codebases and making it easier to accommodate changing requirements or enhancements.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-promotes-teamwork-and-collaboration">Promotes Teamwork and Collaboration</h2>



<p></p>



<p>Teamwork is encouraged in software development initiatives through <a href="https://www.thecodebuzz.com/unit-testing-best-practices/" target="_blank" rel="noopener" title="Unit Test Best Practices in Code Development">unit testing</a>. Individual unit testing makes it simpler for several developers to work on various system components at once.</p>



<p></p>



<p>Thanks to the current unit tests, each developer may be confident that their changes have not caused any regression problems.</p>



<p></p>



<p>Unit tests also act as living documentation, giving developers a clear grasp of the desired behavior of certain pieces of code and enhancing teamwork.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-unit-test-as-code-documentation">Unit Test as Code Documentation </h2>



<p></p>



<p>Unit tests act as living documentation, illustrating how individual units of code should behave. By reading unit tests, developers gain insights into the expected inputs, outputs, and interactions of code components. </p>



<p></p>



<p>This documentation aspect is invaluable when new developers join a project or when revisiting code after a significant period. </p>



<p></p>



<p>Unit tests serve as executable examples, helping developers understand the codebase faster and encouraging adherence to coding standards and best practices.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-create-mock-behavior-describe-and-test-unexpected-behavior">Create Mock behavior &#8211; Describe and test Unexpected behavior </h2>



<p></p>



<p><a href="https://www.thecodebuzz.com/unit-testing-controller-sync-and-async-methods-asp-net-core-example/" target="_blank" rel="noopener" title="Unit Test and Mock Sync/Async Controller Methods in ASP.NET Core">Mocking </a>simplifies the setup process for unit tests by eliminating the need to configure or provision real external systems. </p>



<p></p>



<p>With mock objects or functions, developers can define the expected interactions and outputs without worrying about complex setup procedures. </p>



<p></p>



<p>This simplification accelerates the test preparation phase, reducing the overhead associated with configuring and maintaining test environments. It also makes it easier to replicate.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-controlled-test-scenarios-execution">Controlled Test Scenarios execution</h2>



<p></p>



<p>Unit testing lets you go above and beyond considering testing. </p>



<p>You can go to any extent by unit testing your module and making them great. </p>



<p>For example, Mocking empowers developers to have precise control over various test scenarios. </p>



<p>By creating test objects or functions, developers can define specific behaviors, responses, or inputs for the simulated dependencies. </p>



<p> Developers can systematically cover a wide range of test cases, ensuring comprehensive test coverage and enhancing the overall reliability of the software.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-faster-debugging-and-troubleshooting">Faster Debugging and Troubleshooting</h2>



<p></p>



<p>The particular location of the problem is shown when a unit test fails, making it easier for developers to identify the root cause.</p>



<p></p>



<p>Unit tests act as a safety net while debugging and troubleshooting software.</p>



<p></p>



<p>With less time spent searching through large code bases for issues, developers may more quickly and efficiently correct errors thanks to this targeted approach to debugging.</p>



<p></p>



<p>By reducing the amount of time spent on debugging, unit testing helps developers maintain the development momentum and deliver high-quality software on time.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-test-driven-development-tdd">Test-Driven Development (TDD)</h2>



<p></p>



<p>Unit testing is a core practice in TDD. Developers write tests before implementing the code, using them as a guide to drive the development process and ensure test coverage. </p>



<p>Unit tests act as a form of regression testing, ensuring that previously implemented functionality continues to work as expected after changes or updates.</p>



<p></p>



<p>While unit testing focuses on testing individual units, it also aids integration testing. By isolating units, developers can verify that they interact correctly when combined, detecting issues early in the integration process.</p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-summary">Summary</h2>



<p></p>



<p>Unit testing is an essential method in software development that provides several advantages that enhance the quality of software systems. Unit testing benefits the entire development process by enhancing the quality of the code, enabling early issue identification, accelerating debugging, assisting code refactoring, encouraging collaboration, and offering code documentation. It enables programmers to create durable, maintainable code.</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/top-10-benefits-of-unit-testing-awesome-code/">Top 10 Benefits of Unit Testing – Awesome Code</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/top-10-benefits-of-unit-testing-awesome-code/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Testing &#8211; How to verify that method was not called in Moq?</title>
		<link>https://thecodebuzz.com/how-to-verify-that-method-was-not-called-in-moq-net-core-test-case/</link>
					<comments>https://thecodebuzz.com/how-to-verify-that-method-was-not-called-in-moq-net-core-test-case/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 15 Aug 2022 18:16:00 +0000</pubDate>
				<category><![CDATA[Test Automation]]></category>
		<category><![CDATA[c# moq check if method was called with parameter]]></category>
		<category><![CDATA[c# unit test verify method called without mock]]></category>
		<category><![CDATA[fluent assertions check if method was called]]></category>
		<category><![CDATA[moq method not called]]></category>
		<category><![CDATA[moq verify]]></category>
		<category><![CDATA[moq verify called once]]></category>
		<category><![CDATA[moq verify not working]]></category>
		<category><![CDATA[moq verify property set]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=22705</guid>

					<description><![CDATA[<p>How to verify that method was NOT called in Moq? Today in this article, we will see programmatically verify that method was NOT called in Moq or method invocation is more than 1 or equal to of expected invocation times. While performing unit test cases or functional test execution, sometimes it&#8217;s beneficial to know if [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/how-to-verify-that-method-was-not-called-in-moq-net-core-test-case/">Testing – How to verify that method was not called in Moq?</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">How to verify that method was NOT called in Moq?</h1>



<p>Today in this article, we will see programmatically verify that method was NOT called in Moq or method invocation is more than 1 or equal to of expected invocation times.</p>



<p></p>



<p>While performing unit test cases or functional test execution, sometimes it&#8217;s beneficial to know if particular logic or method was called or not. Especially in business-critical test cases, you may want to go crazy about how much validation you want to add to verify a scenario. This might help in addressing the quality of test cases written and provide additional validation steps.</p>



<p></p>



<p>We will use the below example where we will verify if the Logging method execution happened within the business logic or not.</p>



<p></p>



<p>Below is the sample example that we are calling Lognformation() and LogError().</p>



<p></p>



<p>So above we will verify for a valid scenario if <strong>LogError</strong>() is not called and same time <strong>Lognformation</strong>()  is called 2 times.</p>



<p></p>



<figure class="wp-block-image"><img fetchpriority="high" decoding="async" width="1504" height="924" src="https://i0.wp.com/thecodebuzz.com/wp-content/uploads/2019/11/unit-test-logger-ilogger-net-core-moq-extension-method1.jpg?fit=785%2C482&amp;ssl=1" alt="How to verify that method was NOT called in Moq?" class="wp-image-6458" srcset="https://thecodebuzz.com/wp-content/uploads/2019/11/unit-test-logger-ilogger-net-core-moq-extension-method1.jpg 1504w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-test-logger-ilogger-net-core-moq-extension-method1-300x184.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-test-logger-ilogger-net-core-moq-extension-method1-1024x629.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-test-logger-ilogger-net-core-moq-extension-method1-768x472.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-test-logger-ilogger-net-core-moq-extension-method1-785x482.jpg 785w" sizes="(max-width: 1504px) 100vw, 1504px" /></figure>



<p></p>



<p>Let&#8217;s write Unit test cases as below,</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [23,24,25,26,27,28]; title: ; notranslate">
        &#x5B;Fact]                 
        public void BookService_Get_ValidBookID_Success()
        {

            //Arrange
            var _mockBusiness = new Mock&lt;IBusinessLayer&gt;();
            var _mockLogger = new Mock&lt;ILogger&lt;ValuesController&gt;&gt;();
            int input = 12;
            int expectedloggerInvocationCount = 2;
            string expectedResult = &quot;True&quot;;

            _mockBusiness.Setup(x =&gt; x.PerformBusiness()).Returns(true);

            //Act
            ValuesController controller = new ValuesController(_mockLogger.Object, _mockBusiness.Object);
            var result = controller.Get(input).Result as ObjectResult;

            //Assert

            Assert.Equal(HttpStatusCode.OK, (HttpStatusCode)result.StatusCode);
            Assert.Equal(expectedResult, result.Value);

            _mockLogger.Verify(x =&gt; x.Log(LogLevel.Information, 
                It.IsAny&lt;EventId&gt;(), 
                It.IsAny&lt;It.IsAnyType&gt;(), 
                It.IsAny&lt;Exception&gt;(), 
                (Func&lt;It.IsAnyType, Exception, string&gt;)It.IsAny&lt;object&gt;()), 
                Times.Exactly(2));
      
        }
</pre></div>


<p></p>



<h2 class="wp-block-heading">Verify that method was NOT called in Moq</h2>



<p></p>



<p></p>



<p>You can use <strong><em>XUnit </em></strong>with Moq mock support methods if logger methods are<strong><em> called Once or Twice</em></strong>, etc.</p>



<p></p>



<p>One can use the below code to achieve the same,</p>



<p></p>



<p>As I verified, the below code works for <strong><em>ASP.NET Core 3.1</em></strong> or <strong>.NET 5 </strong>also,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [6]; title: ; notranslate">
 _mockLogger.Verify(x =&gt; x.Log(LogLevel.Error, 
                It.IsAny&lt;EventId&gt;(), 
                It.IsAny&lt;It.IsAnyType&gt;(), 
                It.IsAny&lt;Exception&gt;(), 
                (Func&lt;It.IsAnyType, Exception, string&gt;)It.IsAny&lt;object&gt;()), 
                Times.Exactly(0));
</pre></div>


<p></p>



<p>The above code is verifying if the LogError method was <strong>not called i.e called 0 times.</strong></p>



<p></p>



<p></p>



<h2 class="wp-block-heading">Verify that method was called 1 or &#8216;n&#8217; times in Moq</h2>



<p></p>



<p>The below code is validating that the <strong>LogInformation </strong>is getting called 2 times in a given method.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [6]; title: ; notranslate">
 _mockLogger.Verify(x =&gt; x.Log(LogLevel.Information, 
                It.IsAny&amp;lt;EventId&gt;(), 
                It.IsAny&amp;lt;It.IsAnyType&gt;(), 
                It.IsAny&amp;lt;Exception&gt;(), 
                (Func&amp;lt;It.IsAnyType, Exception, string&gt;)It.IsAny&amp;lt;object&gt;()), 
                Times.Exactly(2));
</pre></div>


<p></p>



<p>This is useful when you are making sure developers don&#8217;t miss to call logging at least 2 times i.e start and at end of the method call. Very useful indeed for debugging and troubleshooting purposes in lower environments.</p>



<p></p>



<p>The above code matches with the below-overloaded Log method template,</p>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="196" src="https://www.thecodebuzz.com/wp-content/uploads/2020/03/Expected-invocation-on-the-mock-exactly-2-times-but-was-0-times-called-Logger-1024x196.jpg" alt="Verify method invocation in XUnit Moq" class="wp-image-8347" srcset="https://thecodebuzz.com/wp-content/uploads/2020/03/Expected-invocation-on-the-mock-exactly-2-times-but-was-0-times-called-Logger-1024x196.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/03/Expected-invocation-on-the-mock-exactly-2-times-but-was-0-times-called-Logger-300x57.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/03/Expected-invocation-on-the-mock-exactly-2-times-but-was-0-times-called-Logger-768x147.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/03/Expected-invocation-on-the-mock-exactly-2-times-but-was-0-times-called-Logger-1536x294.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2020/03/Expected-invocation-on-the-mock-exactly-2-times-but-was-0-times-called-Logger-2048x392.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2020/03/Expected-invocation-on-the-mock-exactly-2-times-but-was-0-times-called-Logger-785x150.jpg 785w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p><strong>References </strong>: </p>



<p><a href="http://twitter.com/intent/tweet?status=Unit%20Test%20Naming%20Conventions%20Best%20Practices+https://www.thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/" target="_blank" rel="noreferrer noopener"><br></a><a href="https://plus.google.com/share?url=https://www.thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/" target="_blank" rel="noreferrer noopener"></a><a href="https://pinterest.com/pin/create/button/?url=https://www.thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/&amp;media=&amp;description=Unit%20Test%20Naming%20Conventions%20Best%20Practices" target="_blank" rel="noreferrer noopener"></a></p>



<ul class="has-medium-font-size wp-block-list"><li><a href="https://www.thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/" target="_blank" rel="noopener">Unit Test Naming Conventions Best Practices</a></li></ul>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/how-to-verify-that-method-was-not-called-in-moq-net-core-test-case/">Testing – How to verify that method was not called in Moq?</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/how-to-verify-that-method-was-not-called-in-moq-net-core-test-case/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Unit Test Naming Conventions Best Practices</title>
		<link>https://thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/</link>
					<comments>https://thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 15 Dec 2021 23:49:00 +0000</pubDate>
				<category><![CDATA[Test Automation]]></category>
		<category><![CDATA[XP Practices]]></category>
		<category><![CDATA[Define naming for Test Class]]></category>
		<category><![CDATA[Define naming for Test Files]]></category>
		<category><![CDATA[integration test naming convention]]></category>
		<category><![CDATA[jest unit test naming convention]]></category>
		<category><![CDATA[manual test case naming convention]]></category>
		<category><![CDATA[name test method]]></category>
		<category><![CDATA[Naming Conventions for Test method]]></category>
		<category><![CDATA[naming test method]]></category>
		<category><![CDATA[test case naming conventions best practices]]></category>
		<category><![CDATA[unit test best practices]]></category>
		<category><![CDATA[unit test naming convention c#]]></category>
		<category><![CDATA[unit test naming convention given when then]]></category>
		<category><![CDATA[unit test naming convention python]]></category>
		<category><![CDATA[Unit Testing Naming Conventions and Standards Tests]]></category>
		<category><![CDATA[use AAA pattern unit test]]></category>
		<category><![CDATA[use given when given pattern test cases.]]></category>
		<category><![CDATA[xunit name test method]]></category>
		<guid isPermaLink="false">https://thecodebuzz.com/?p=6698</guid>

					<description><![CDATA[<p>TDD &#8211; Unit Test Naming Conventions Best Practices I’m going to talk about simple and easy-to-understand Unit Test Naming Conventions and Best Practices that can be followed while writing Unit Testing. As we know Naming standards in any format for anything ( code, documents, unit tests, etc) are always helpful in the long run. The [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/">Unit Test Naming Conventions Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>TDD &#8211; Unit Test Naming Conventions Best Practices</strong></h1>



<figure class="wp-block-image"><a href="https://www.thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/" target="_blank" rel="noopener"><img decoding="async" src="https://i0.wp.com/thecodebuzz.com/wp-content/uploads/2019/01/Unit-Testing-Best-Practices-1.jpg?fit=785%2C349&amp;ssl=1" alt="Unit Testing Naming Conventions and Standards"/></a></figure>



<p>I’m going to talk about simple and easy-to-understand Unit Test Naming Conventions and Best Practices that can be followed while writing Unit Testing. </p>



<p></p>



<p>As we know Naming standards in any format for anything ( code, documents, unit tests, etc) are always helpful in the long run. The naming standard brings uniformity and avoids ambiguity.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-developers-confusion-on-naming">Developer&#8217;s Confusion on Naming ??</a></li><li><a href="#aioseo-naming-pattern-for-unit-testing">Naming Pattern for Unit Testing</a></li><li><a href="#aioseo-unit-test-project-naming-convention">Unit Test Project Naming Convention</a><ul><li><a href="#aioseo-app-name-unit-tests">[App-Name].Unit.Tests</a></li></ul></li><li><a href="#aioseo-unit-test-files-naming-convention">Unit Test Files Naming Convention</a><ul><li><a href="#aioseo-classnametests">[ClassName]Tests</a></li></ul></li><li><a href="#aioseo-unit-test-class-naming-convention">Unit Test Class Naming Convention</a><ul><li><a href="#aioseo-classnametests">[ClassName]Tests</a></li></ul></li><li><a href="#aioseo-unit-test-method-naming-convention">Unit Test Method Naming Convention</a><ul><li><a href="#aioseo-pattern">Pattern :</a></li><li><a href="#aioseo-unitofwork_method_behaviour">[UnitOfWork]_[Method]_[Behaviour]</a></li></ul></li><li><a href="#aioseo-unit-test-structure">Unit Test Structure</a><ul><li><a href="#aioseo-pattern">Pattern:</a><ul><li><a href="#aioseo-act---arrange---assert">Act &#8211; Arrange &#8211; Assert</a></li><li><a href="#aioseo-given-when-then">Given-When-Then</a></li></ul></li></ul></li><li><a href="#aioseo-summary">Summary</a></li></ul></div>



<p></p>



<p>Coming to Unit testing naming, however, we don&#8217;t really need to get crazy. However, by following them we get multiple benefits. </p>



<p></p>



<p>Following such naming doesn&#8217;t require any extra effort, as once put into practice, you enjoy all their benefits.</p>



<p></p>



<p>In our last article, we understood general <a href="https://www.thecodebuzz.com/unit-testing-best-practices/" target="_blank" rel="noreferrer noopener" title="Unit Test Best Practices in Software Development"><em><strong>best practices for writing the unit test cases</strong></em></a>. </p>



<p></p>



<p>However today in this article we will focus primarily on the naming conventions </p>



<p></p>



<p>Today we will see how to use the naming convention for,</p>



<p></p>



<ul class="wp-block-list">
<li>Test Method</li>
</ul>



<ul class="wp-block-list">
<li>Test Class</li>
</ul>



<ul class="wp-block-list">
<li>Test Project</li>
</ul>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-developers-confusion-on-naming">Developer&#8217;s Confusion on Naming ??</h2>



<p></p>



<p>This is a small but very common problem among developers. </p>



<p></p>



<p>There is always confusion in the Developer community on how to name and structure Unit Test cases properly. </p>



<p></p>



<p>Most often developer ends up writing their test cases without any guidelines resulting in issues like tests are not readable, not properly structured, and difficult in troubleshooting, especially on the build server where thousands of test cases run daily.</p>



<p></p>



<figure class="wp-block-image size-large"><a href="https://www.thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/" target="_blank" rel="noopener"><img decoding="async" width="1607" height="924" src="https://i0.wp.com/thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-1.jpg?fit=785%2C452&amp;ssl=1" alt="Unit Testing Naming Standards" class="wp-image-6736" srcset="https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-1.jpg 1607w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-1-300x172.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-1-1024x589.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-1-768x442.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-1-1536x883.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-1-785x451.jpg 785w" sizes="(max-width: 1607px) 100vw, 1607px" /></a></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-naming-pattern-for-unit-testing">Naming Pattern for Unit Testing</h2>



<p></p>



<p>As we discussed in our last article on Unit testing best practices, it&#8217;s important to understand that while writing actual unit test cases everything is drilled down to finally <strong>Classes </strong>and <strong>Functions</strong>. </p>



<p></p>



<p></p>



<p>These two entities are required to be unit tested in isolation for a given functionality implemented in methods.</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://i2.wp.com/thecodebuzz.com/wp-content/uploads/2019/11/UnitTest-Best-practices-flow.png?fit=785%2C393&amp;ssl=1" alt="Unit Testing Naming Conventions and Standards" class="wp-image-6710" width="454" height="227" srcset="https://thecodebuzz.com/wp-content/uploads/2019/11/UnitTest-Best-practices-flow.png 1535w, https://thecodebuzz.com/wp-content/uploads/2019/11/UnitTest-Best-practices-flow-300x150.png 300w, https://thecodebuzz.com/wp-content/uploads/2019/11/UnitTest-Best-practices-flow-1024x513.png 1024w, https://thecodebuzz.com/wp-content/uploads/2019/11/UnitTest-Best-practices-flow-768x385.png 768w, https://thecodebuzz.com/wp-content/uploads/2019/11/UnitTest-Best-practices-flow-785x393.png 785w" sizes="auto, (max-width: 454px) 100vw, 454px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-unit-test-project-naming-convention">Unit Test Project Naming Convention</h2>



<p></p>



<p>The below examples are just guidelines to help you with the naming pattern for the Test project.</p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>You are free to use your own customized naming pattern as required. Below are just few suggestions based on my experience. 😉</p>
</blockquote>



<p></p>



<p><strong>Pattern: </strong></p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-app-name-unit-tests">[App-Name].Unit.Tests</h3>



<p></p>



<p>Example: BooksApi.Unit.Tests</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="998" height="273" src="https://www.thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-project-names.jpg" alt="Unit Testing Naming Conventions and Standards" class="wp-image-6724" srcset="https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-project-names.jpg 998w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-project-names-300x82.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-project-names-768x210.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-project-names-785x215.jpg 785w" sizes="auto, (max-width: 998px) 100vw, 998px" /></figure>



<p>Other conventions like  BooksApi-Unit-Tests can also be used if found suitable.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-unit-test-files-naming-convention">Unit Test Files Naming Convention </h2>



<p></p>



<p>Files can be named after the class names you are dealing with &#8230;</p>



<p></p>



<p><strong>Pattern:</strong></p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-classnametests">[ClassName]Tests</h3>



<p></p>



<p><strong>Example:</strong> BookServiceTests.cs</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-file.jpg" alt="Unit test name example" class="wp-image-6725" width="449" height="530" srcset="https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-file.jpg 567w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-file-254x300.jpg 254w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-file-441x520.jpg 441w" sizes="auto, (max-width: 449px) 100vw, 449px" /></figure>



<p></p>



<p><strong>Tips:</strong></p>



<ul class="wp-block-list">
<li>Every class with the public method (at least one method) should have a test file created. (This also means you should refactor your files as per classes)</li>
</ul>



<ul class="wp-block-list">
<li>Make sure to write unit test cases for all public APIs for each given class.</li>
</ul>



<ul class="wp-block-list">
<li>Each test file should be completely isolated from other files.</li>
</ul>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-unit-test-class-naming-convention">Unit Test Class Naming Convention </h2>



<p></p>



<p>You need a test class to encapsulate all your test cases. This test class provides an entry point to test cases depending on the configuration. </p>



<p>This also helps to initialize the test data required for test cases.</p>



<p></p>



<p><strong>Pattern:</strong></p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-classnametests">[ClassName]Tests</h3>



<p></p>



<p>Example:  &#8216;EmployeesTests&#8217; where Employee is a class name.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="2113" height="912" src="https://i0.wp.com/thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-file-names.jpg?fit=785%2C339&amp;ssl=1" alt="" class="wp-image-6727" srcset="https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-file-names.jpg 2113w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-file-names-300x129.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-file-names-1024x442.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-file-names-768x331.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-file-names-1536x663.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-file-names-2048x884.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-file-names-785x339.jpg 785w" sizes="auto, (max-width: 2113px) 100vw, 2113px" /></figure>



<p></p>



<p><strong>Tips:</strong></p>



<p></p>



<ul class="wp-block-list">
<li>Name your Test Class as per the business class name. </li>



<li>Each Test Class should be defined in a separate test file as discussed above.</li>



<li>Each Test Class should have a mechanism to initialize mock or test data</li>



<li>Each test class should be public.</li>
</ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-unit-test-method-naming-convention">Unit Test Method Naming Convention</h2>



<p></p>



<p>Each Unit test method should be unit tested in isolation. This isolation can be simplified by a Class and all its methods.</p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-pattern">Pattern :</h3>



<p></p>



<h3 class="wp-block-heading" id="aioseo-unitofwork_method_behaviour">[UnitOfWork]_[Method]_[Behaviour]</h3>



<p></p>



<p>Example: BookService_GetBookAsync_InValidBookID_Failure</p>



<p></p>



<figure class="wp-block-image size-large"><a href="https://www.thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="1142" height="531" src="https://i2.wp.com/thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-name-1.jpg?fit=785%2C365&amp;ssl=1" alt="Unit Test Naming Conventions Best Practices" class="wp-image-6730" srcset="https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-name-1.jpg 1142w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-name-1-300x139.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-name-1-1024x476.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-name-1-768x357.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-name-1-785x365.jpg 785w" sizes="auto, (max-width: 1142px) 100vw, 1142px" /></a></figure>



<h2 class="wp-block-heading" id="aioseo-unit-test-structure">Unit Test Structure </h2>



<p></p>



<p>One can follow the AAA or  Given-When-Then style pattern while structuring your test cases.</p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-pattern">Pattern:</h3>



<p></p>



<h4 class="wp-block-heading" id="aioseo-act---arrange---assert">Act &#8211; Arrange &#8211; Assert</h4>



<p></p>



<p>The essential idea of the <strong>AAA </strong>pattern is to break down writing your test into three sections.</p>



<p></p>



<ul class="wp-block-list">
<li><strong>Arrange</strong>: This describes a preconditioned state of the test before you begin to test the behavior you&#8217;re specifying in this scenario.  Helps you create required test preparations, test data, mocking, etc.</li>
</ul>



<ul class="wp-block-list">
<li><strong>Act</strong>&#8211; This section tests the behavior that you&#8217;re specifying. </li>
</ul>



<ul class="wp-block-list">
<li><strong>Assert</strong>&#8211; This section describes the changes you expect due to the defined inputs through arranging and specified behavior through Act. </li>
</ul>



<p></p>



<figure class="wp-block-image size-large is-resized"><a href="https://www.thecodebuzz.com/integration-testing-best-practices-agile/" target="_blank" rel="noopener"><img loading="lazy" decoding="async" src="https://i0.wp.com/thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-structure.jpg?fit=785%2C643&amp;ssl=1" alt="Unit Test Naming Conventions Best Practices" class="wp-image-6733" width="569" height="466" srcset="https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-structure.jpg 1128w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-structure-300x246.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-structure-1024x839.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-structure-768x629.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/11/unit-testing-naming-conventions-for-test-method-structure-635x520.jpg 635w" sizes="auto, (max-width: 569px) 100vw, 569px" /></a></figure>



<p></p>



<p>Some of you might like one more pattern as below,</p>



<p></p>



<p><strong>Pattern</strong>:</p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-given-when-then">Given-When-Then&nbsp; </h4>



<p></p>



<figure class="wp-block-image size-large is-resized"><a href="https://www.thecodebuzz.com/integration-testing-best-practices-agile/" target="_blank" rel="noopener"><img loading="lazy" decoding="async" src="https://i0.wp.com/thecodebuzz.com/wp-content/uploads/2019/11/image-15.png?fit=785%2C654&amp;ssl=1" alt="Unit Test Naming Conventions Best Practices" class="wp-image-6740" width="561" height="468" srcset="https://thecodebuzz.com/wp-content/uploads/2019/11/image-15.png 1109w, https://thecodebuzz.com/wp-content/uploads/2019/11/image-15-300x250.png 300w, https://thecodebuzz.com/wp-content/uploads/2019/11/image-15-1024x853.png 1024w, https://thecodebuzz.com/wp-content/uploads/2019/11/image-15-768x640.png 768w, https://thecodebuzz.com/wp-content/uploads/2019/11/image-15-624x520.png 624w" sizes="auto, (max-width: 561px) 100vw, 561px" /></a></figure>



<p></p>



<p>The above pattern has the same meaning on the structure as defined above for the <strong>AAA </strong>pattern. </p>



<p><strong><em>Given-When-Then</em></strong> is most popular in Behavior-Driven Development&nbsp;(<em><strong>BDD</strong></em>) and Functional Testing.</p>



<p></p>



<p>Do you find the above conventions helpful?</p>



<p></p>



<p><strong><em>References: </em></strong></p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/integration-testing-best-practices-agile/" title="Integration Testing Best Practices in Agile" target="_blank" rel="noreferrer noopener"><strong><em>Integration Testing Best Practices in Agile</em></strong></a></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/unit-testing-best-practices/" target="_blank" rel="noreferrer noopener" title="Unit Test Best Practices in Software Development"><strong><em>Top 10 Unit Test Best Practices in Software Development</em></strong></a></li>
</ul>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-summary">Summary</h2>



<p></p>



<p>In this post, we learned a few basic and easy-to-use naming conventions for Unit Testing. We looked at the different naming convention which is useful for Test Project, Test Files, Test Class, Test method and Test method structure, etc.</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/">Unit Test Naming Conventions Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/tdd-unit-testing-naming-conventions-and-standards/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Unit Test and Mock ServiceFilter Attribute of Controllers</title>
		<link>https://thecodebuzz.com/unit-test-mock-servicefilter-attribute-controllers-asp-net-api/</link>
					<comments>https://thecodebuzz.com/unit-test-mock-servicefilter-attribute-controllers-asp-net-api/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 06 Dec 2021 04:14:00 +0000</pubDate>
				<category><![CDATA[Test Automation]]></category>
		<category><![CDATA[.net core unit test mock]]></category>
		<category><![CDATA[actiondescriptor unit test]]></category>
		<category><![CDATA[how to unit test mvc controller c#]]></category>
		<category><![CDATA[mock actioncontext]]></category>
		<category><![CDATA[onactionexecutionasync]]></category>
		<category><![CDATA[resultexecutingcontext unit test]]></category>
		<category><![CDATA[unit test actionfilterattribute]]></category>
		<category><![CDATA[unit test api controller c#]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=19647</guid>

					<description><![CDATA[<p>Unit Test and Mock ServiceFilter Attribute of Controllers Today in this article we shall see how to Unit Test and Mock ServiceFilter Attributes of Controllers and write better unit test cases for a controller with a service filter attribute. Unit test cases of controller-specific methods make API robust and help identify any breaking changes early. [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/unit-test-mock-servicefilter-attribute-controllers-asp-net-api/">Unit Test and Mock ServiceFilter Attribute of Controllers</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">Unit Test and Mock ServiceFilter Attribute of Controllers</h1>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2021/12/Unit-Test-and-Mock-ServiceFilter-Attribute-of-Controllers-1024x394.jpg" alt="Mock ServiceFilter" class="wp-image-19654" width="788" height="303" srcset="https://thecodebuzz.com/wp-content/uploads/2021/12/Unit-Test-and-Mock-ServiceFilter-Attribute-of-Controllers-1024x394.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/12/Unit-Test-and-Mock-ServiceFilter-Attribute-of-Controllers-300x115.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/12/Unit-Test-and-Mock-ServiceFilter-Attribute-of-Controllers-768x296.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/12/Unit-Test-and-Mock-ServiceFilter-Attribute-of-Controllers-785x302.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/12/Unit-Test-and-Mock-ServiceFilter-Attribute-of-Controllers.jpg 1351w" sizes="auto, (max-width: 788px) 100vw, 788px" /></figure>



<p>Today in this article we shall see how to Unit Test and Mock ServiceFilter Attributes of Controllers and write better unit test cases for a controller with a service filter attribute.</p>



<p></p>



<p>Unit test cases of controller-specific methods make API robust and help identify any breaking changes early.</p>



<p></p>



<p>We shall use below sample code below where the controller is attributed with filter attribute i.e <a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.servicefilterattribute" target="_blank" rel="noopener" title="">ServiceFilter </a>attribute as shown below example,</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="196" src="https://www.thecodebuzz.com/wp-content/uploads/2021/12/Mock-ServiceFilter-Attribute-of-Controllers-1-1024x196.jpg" alt="Unit Test and Mock ServiceFilter Attribute of Controllers" class="wp-image-19649" srcset="https://thecodebuzz.com/wp-content/uploads/2021/12/Mock-ServiceFilter-Attribute-of-Controllers-1-1024x196.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/12/Mock-ServiceFilter-Attribute-of-Controllers-1-300x57.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/12/Mock-ServiceFilter-Attribute-of-Controllers-1-768x147.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/12/Mock-ServiceFilter-Attribute-of-Controllers-1-1536x294.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/12/Mock-ServiceFilter-Attribute-of-Controllers-1-785x150.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/12/Mock-ServiceFilter-Attribute-of-Controllers-1.jpg 1831w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>The controller&nbsp;with action filters includes the below-listed&nbsp;methods. These methods wrap the filters that run for a given action,</p>



<p></p>



<ul class="has-medium-font-size wp-block-list">
<li><code>OnActionExecuting</code>&nbsp;&#8211; runs before any of the action&#8217;s filters.</li>



<li><code>OnActionExecuted</code>&nbsp;&#8211; runs after all of the action&#8217;s filters.</li>



<li><code>OnActionExecutionAsync</code>&#8211; async format </li>
</ul>



<p></p>



<p>Below is the actual implementation of the <strong><em>ValidateInputAttribute</em></strong>,</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="531" src="https://www.thecodebuzz.com/wp-content/uploads/2021/12/Unit-test-ServiceFilter-Attribute-of-Controllers-1024x531.jpg" alt="Unit Test and Mock ServiceFilter Attribute of Controllers" class="wp-image-19650" srcset="https://thecodebuzz.com/wp-content/uploads/2021/12/Unit-test-ServiceFilter-Attribute-of-Controllers-1024x531.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/12/Unit-test-ServiceFilter-Attribute-of-Controllers-300x156.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/12/Unit-test-ServiceFilter-Attribute-of-Controllers-768x398.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/12/Unit-test-ServiceFilter-Attribute-of-Controllers-1536x797.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/12/Unit-test-ServiceFilter-Attribute-of-Controllers-785x407.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/12/Unit-test-ServiceFilter-Attribute-of-Controllers.jpg 1992w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>Lets now create the Unit test case for this action filter attribute which of type ServiceFilter</p>



<p></p>



<p><strong>Step 1- Create a Mock of ActionContext</strong></p>



<p></p>



<p>Below is a simple way of creating a mock of ActionContext. This mock object will be needed in the next steps where we will create a mock setup for <strong><em>ActionExecutingContext</em></strong>.</p>



<p></p>



<p>Below we are creating a mock object of  HttpContext,  RouteData, and  ActionDescriptor using the <strong>moq </strong>library.</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"> var actionContext = new ActionContext(
                Mock.Of&lt;HttpContext&gt;(),
                Mock.Of&lt;RouteData&gt;(),
                Mock.Of&lt;ActionDescriptor&gt;(),
                Mock.Of&lt;ModelStateDictionary&gt;()
            );</pre>



<p></p>



<p><strong>Step 2- Create a Mock of ActionExecutingContext</strong></p>



<p></p>



<p>Let&#8217;s now create the mock for <strong>ActionExecutingContext </strong>using the mock action context object as defined above. We are also passing a mock list of IFilterMetadata and Dictionary key-value pairs.</p>



<p></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">var actionExecutingContext = new ActionExecutingContext(
                actionContext,
                new List&lt;IFilterMetadata&gt;(),
                new Dictionary&lt;string, object&gt;(),
                Mock.Of&lt;Controller&gt;()
            );</pre>



<p></p>



<p><strong>Step 3: Define the action </strong></p>



<p></p>



<p>Let&#8217;s define the action by creating the instance of <strong><em>ValidateInputAttribute </em></strong>and executing the <em><strong>OnActionExecuting </strong></em>method,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"> var valInputObject = new ValidateInputAttribute &lt;string&gt;();
 valInputObject.OnActionExecuting(actionExecutingContext);
</pre>



<p></p>



<p></p>



<p><strong>Step 4: Define Assert</strong></p>



<p></p>



<p>We are making sure the result of validation fails reports <strong><em>BadRequestObjectResult </em></strong>as shown below,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">Assert.IsType&lt;BadRequestObjectResult&gt;(actionExecutingContext.Result);

</pre>



<p><strong>Useful References </strong></p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/restful-api-controller-unit-testing-best-practices/" target="_blank" rel="noreferrer noopener" title="RESTFul API/Controller Unit Testing – Best Practices"><strong><em>RESTFul API/Controller Unit Testing – Best Practices</em></strong></a></li>
</ul>



<p></p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/unit-test-mock-servicefilter-attribute-controllers-asp-net-api/">Unit Test and Mock ServiceFilter Attribute of Controllers</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/unit-test-mock-servicefilter-attribute-controllers-asp-net-api/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>This task requires that the user account specified has Log on as batch job rights</title>
		<link>https://thecodebuzz.com/this-task-requires-that-the-user-account-specified-has-log-on-as-batch-job-rights/</link>
					<comments>https://thecodebuzz.com/this-task-requires-that-the-user-account-specified-has-log-on-as-batch-job-rights/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 03 Jul 2021 14:31:29 +0000</pubDate>
				<category><![CDATA[Test Automation]]></category>
		<category><![CDATA[Log on as batch job rights]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=17065</guid>

					<description><![CDATA[<p>This ask requires that the user account specified has Log on as batch job rights Today in this article, we will cover below aspects, Issue Description While running Task scheduler configured jobs on the Windows server produce the below error, &#8220;This Task requires that the user account specified has Log on as batch job rights&#8221; [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/this-task-requires-that-the-user-account-specified-has-log-on-as-batch-job-rights/">This task requires that the user account specified has Log on as batch job rights</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">This ask requires that the user account specified has Log on as batch job rights</h1>



<p>Today in this article, we will cover below aspects,</p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-issue-description">Issue Description</a></li><li><a href="#aioseo-resolution">Resolution</a></li><li><a href="#aioseo-open-local-security-policy">Open Local Security Policy</a></li><li><a href="#aioseo-click-on-security-settings-user-rights-assignment">Click on Security Settings -> User Rights Assignment</a></li></ul></div>



<h2 class="wp-block-heading" id="aioseo-issue-description">Issue Description</h2>



<p></p>



<p>While running Task scheduler configured jobs on the Windows server produce the below error,</p>



<p></p>



<p class="has-vivid-red-color has-text-color has-medium-font-size">&#8220;This Task requires that the user account specified has Log on as batch job rights&#8221;</p>



<p></p>



<figure class="wp-block-image size-large"><a href="https://www.thecodebuzz.com/this-task-requires-that-the-user-account-specified-has-log-on-as-batch-job-rights/" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="485" height="182" src="https://www.thecodebuzz.com/wp-content/uploads/2021/07/This-task-requires-that-the-user-account-specified-has-Log-on-as-batch-job-rights-2.jpg" alt="Log on as batch job rights" class="wp-image-17071" srcset="https://thecodebuzz.com/wp-content/uploads/2021/07/This-task-requires-that-the-user-account-specified-has-Log-on-as-batch-job-rights-2.jpg 485w, https://thecodebuzz.com/wp-content/uploads/2021/07/This-task-requires-that-the-user-account-specified-has-Log-on-as-batch-job-rights-2-300x113.jpg 300w" sizes="auto, (max-width: 485px) 100vw, 485px" /></a></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-resolution">Resolution</h2>



<p></p>



<p>To resolve the error, please go to your machine&#8217;s local security policy.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-open-local-security-policy">Open Local Security Policy</h2>



<p></p>



<p>To open Local Security Policy, on the&nbsp;<strong>Start</strong>&nbsp;screen, type&nbsp;the below command,</p>



<p></p>



<p>Command</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">secpol.msc </pre>



<p></p>



<p>and then press <strong>ENTER</strong>.</p>



<p></p>



<p>You can also use regular search using <strong><em>&#8220;Local Security Policy&#8221;</em></strong> in windows search,</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-click-on-security-settings-user-rights-assignment">Click on Security Settings -&gt; User Rights Assignment </h2>



<p></p>



<p>Under User Rights Assignment, please search for <strong><em>&#8220;Log on as a batch job&#8221;</em></strong></p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2021/07/the-user-account-specified-has-Log-on-as-batch-job-rights-1.jpg" alt="Log on as batch job rights" class="wp-image-17072" width="526" height="265" srcset="https://thecodebuzz.com/wp-content/uploads/2021/07/the-user-account-specified-has-Log-on-as-batch-job-rights-1.jpg 720w, https://thecodebuzz.com/wp-content/uploads/2021/07/the-user-account-specified-has-Log-on-as-batch-job-rights-1-300x151.jpg 300w" sizes="auto, (max-width: 526px) 100vw, 526px" /></figure>



<p></p>



<p>Click on the option <strong><em>&#8220;Log on as a batch job&#8221;</em></strong></p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2021/07/user-account-batch-job-role-1.jpg" alt="This task requires that the user account specified has Log on " class="wp-image-17073" width="297" height="354" srcset="https://thecodebuzz.com/wp-content/uploads/2021/07/user-account-batch-job-role-1.jpg 422w, https://thecodebuzz.com/wp-content/uploads/2021/07/user-account-batch-job-role-1-252x300.jpg 252w" sizes="auto, (max-width: 297px) 100vw, 297px" /></figure>



<p></p>



<p>Click on the <strong>Add User group</strong> and add the user name. You can search for the actual username based on the username or email if any.</p>



<p></p>



<p>That&#8217;s all, save your settings and you are all set to run the job with the privileges <strong><em>your Id</em></strong> has.</p>



<p></p>



<p><strong>References:</strong></p>



<p></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/schedule-powershell-script-using-windows-task-scheduler/" target="_blank" rel="noreferrer noopener" title="Schedule PowerShell Script using Windows Task Scheduler"><strong><em>Schedule PowerShell Script using Windows Task Scheduler</em></strong></a></li></ul>



<p></p>



<p></p>



<p>Did I miss anything else in these resolution steps?</p>



<p>Did the above steps resolve your issue? Please sound off your comments below!</p>



<p>Happy Coding !!</p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/this-task-requires-that-the-user-account-specified-has-log-on-as-batch-job-rights/">This task requires that the user account specified has Log on as batch job rights</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/this-task-requires-that-the-user-account-specified-has-log-on-as-batch-job-rights/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
