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.
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.
- 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.
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
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.
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);
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.
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