Amazing features of mocking framework in .NET for the Developers.

Jitendrasoft09
Posted by in .NET Framework category on for Beginner level | Points: 250 | Views : 1445 red flag
Rating: 5 out of 5  
 1 vote(s)

In this article, features of mocking framework in .NET for the Developers who can incorporate the test method using LINQ query quickly and easily without dependencies like database, services and XML serialization on network.

Introduction


Nowadays, mocking framework is used to test the application same as N Unit test which is probably tested by Developers.  There is a difference between Mock and NUnit test. Mock test ignores the code test for outside scenarios where dependencies having such as web service, database and XML on outside network. Mock sends a fake response to pass the test methods from outside network source. Developer wants to cover the test for the method without hitting to web service, database and external sources. Whereas NUnit test requires to run the external sources to pass the test methods.


Features


  • Quick test.
  • Easier to test.
  • Automated specification based test.
  • Integrity of the code.
  • Avoiding complexities of the code while testing.
  • Easy to filer the test method with LINQ Lambda expression.


Using the code

Let us start to create a sample mock test project in MVC application. When we create an MVC application. We need to select test project as below shown in screen shot.


By default NUnit test libraries are persist into visual studio. We have to add the Mock library in visual studio. Please have a look into below link where mock library can be downloaded.

https://code.google.com/p/moq/downloads/detail?name=Moq.4.0.10827.Final.zip&can=2&q=


Here we have added the mock dll and there are some NNunit code test is written already to test the Index action method of Home controller class.


As of now we have seen the mock test code is written to test the service is called or not. Here we are not bother of the service will call or not. Just here fake value is passed to success the test method. That is the advantages of mock testing. We are not dependent to services or other data sources responses to run the test methods.

The below code is written where Mock is used to pass the fake results. Setup is used to call the members of the interface and passed boolean true to get the response from service mean service is called.

 [TestMethod]
        public void Contact()
        {
            HomeController controller = new HomeController();
            ViewResult result = controller.Contact() as ViewResult;
            var mocktest = new Mock<IServiceManager>();
            mocktest.Setup(m => m.IsServiceCalled()).Returns(true);
            Assert.AreEqual(mocktest,result);
        }


Let’s we will see how we can know our test is passed or failed. Let’s see two approaches to test the contact action method. We can use any action method from controller classes.

Approach 1: Assert.AreNotEqual(mocktest,result);

When we run the test contact method which is passed and shown in below screen shot.


Approach 2: Assert.AreEqual(mocktest,result);

When we run the test contact method which is failed and shown in below screen shot.



Conclusion


In this section, we will see  in a short description as we have learned the how Mock test works in Visual Studio to test method faster and easily without any dependencies. So here without running the services we have passed  the test methods. The code is prepared to know how actually Mock works and we can go ahead to test the method for the xml, database and another network data sources. Hope this article will help you to understand the basic knowledge of the mocking framework.  Thanks for the reading



Page copy protected against web site content infringement by Copyscape

About the Author

Jitendrasoft09
Full Name: Jitendra Kumar
Member Level:
Member Status: Member,MVP
Member Since: 3/26/2013 2:40:53 AM
Country: India
Jitendra Kumar If my post helps you, plz mark as an answer.
http://www.dotnetfunda.com/
Jitendra Kumar

Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 2/23/2015 | Points: 25
Excellent article Jitendra. Thanks.

5 from me !

Thanks again.
Posted by: Jitendrasoft09 on: 2/16/2015 | Points: 25
Thanks Sheo for your appreciation.

Login to post response

Comment using Facebook(Author doesn't get notification)