Hello All
In test driven development its always needed to test a particular project tested with make it isolate ( test with removing dependency from other projects ), In this scenario we virtually create the input set which will be produced from other projects(class,methods etc) and test whether our testable methods are functioning with that input set or not.
To do that we have to make mock of the dependent objects. In the example bellow I have shown how to test a business logic layer with isolating it from data access layer.I have created three projects Web.Data , Web.Core & BL.Test .
The Web.Data project contains the Data Access Layer which will be responsible for pulling data from the data source,but as we are isolating our business logic layer from this project so in the example I will not completely implement our desired method( as it will not be executed).
To do that we need some tools such as NMock and NUnit . We will found the NMock.dll , nunit.core & nunit.framework in both of its bin folder where we extract or install those.
our Web.Data contains only two files one is IMockDAL.cs interface and MockDAL.cs class.
IMockDAL.cs interface :
1 2 3 |
<span style="color: blue">using </span>System.Collections.Generic;<br /><br /><span style="color: blue">namespace </span>Web.Data<br />{<br /> <span style="color: blue">public interface </span><span style="color: #2b91af">IMockDAL<br /> </span>{<br /> <span style="color: green">/* The method will return a string list of<br /> * active member's name from data source .<br /> */<br /> </span><span style="color: #2b91af">List</span><<span style="color: blue">string</span>> GetNameListDAL(<span style="color: blue">bool </span>active);<br /> }<br />} |
MockDAL.cs Class :
1 2 3 |
<span style="color: blue">using </span>System;<br /><span style="color: blue">using </span>System.Collections.Generic;<br /><br /><span style="color: blue">namespace </span>Web.Data<br />{<br /> <span style="color: blue">public class </span><span style="color: #2b91af">MockDAL</span>:<span style="color: #2b91af">IMockDAL<br /> </span>{<br /> <span style="color: blue">#region </span>IMockDAL Members<br /><br /> <span style="color: green">/* This method will not be executed during the test,<br /> * as we will do mock of this method. <br /> */<br /> </span><span style="color: blue">public </span><span style="color: #2b91af">List</span><<span style="color: blue">string</span>> GetNameListDAL(<span style="color: blue">bool </span>active)<br /> {<br /> <span style="color: blue">throw new </span><span style="color: #2b91af">NotImplementedException</span>();<br /> }<br /><br /> <span style="color: blue">#endregion<br /> </span>}<br />} |
In our Web.Core there are also two files , one is IEmployeeService.cs interface & EmployeeService.cs the class file. in this project we have to add reference of Web.Data project as it has a dependency over that.
IEmployeeService.cs interface :
1 2 3 |
<span style="color: blue">using </span>System.Collections.Generic;<br /><span style="color: blue">using </span>Web.Data;<br /><br /><span style="color: blue">namespace </span>Web.Core<br />{<br /> <span style="color: blue">public interface </span><span style="color: #2b91af">IEmployeeService<br /> </span>{<br /> <span style="color: #2b91af">IMockDAL </span>MockDAL { <span style="color: blue">set</span>; <span style="color: blue">get</span>; }<br /> <span style="color: #2b91af">List</span><<span style="color: blue">string</span>> GetAllEmployee();<br /> }<br />} |
EmployeeService.cs Class :
1 2 3 |
<span style="color: blue">using </span>System.Collections.Generic;<br /><span style="color: blue">using </span>Web.Data;<br /><br /><span style="color: blue">namespace </span>Web.Core<br />{<br /> <span style="color: blue">public class </span><span style="color: #2b91af">EmployeeService </span>: <span style="color: #2b91af">IEmployeeService<br /> </span>{<br /> <span style="color: green">/* The MockDAL property will be used to assign<br /> * DAL through our mock object.<br /> */<br /> </span><span style="color: blue">public </span><span style="color: #2b91af">IMockDAL </span>MockDAL { <span style="color: blue">set</span>; <span style="color: blue">get</span>; }<br /><br /> <span style="color: blue">public </span>EmployeeService()<br /> {<br /> MockDAL = <span style="color: blue">new </span><span style="color: #2b91af">MockDAL</span>(); <br /> }<br /><br /> <span style="color: blue">#region </span>IEmployeeService Members<br /><br /> <span style="color: green">/* The "GetAllEmploye" will call the "GetNameListDAL"<br /> * from the mock object. For the sake of simplicity <br /> * I just call the DAL,not did any other operation.<br /> */<br /><br /> </span><span style="color: blue">public </span><span style="color: #2b91af">List</span><<span style="color: blue">string</span>> GetAllEmployee()<br /> {<br /> <span style="color: blue">return </span>MockDAL.GetNameListDAL(<span style="color: blue">true</span>);<br /> }<br /><br /> <span style="color: blue">#endregion<br /> </span>}<br />} |
In BL.Test project we have to add some reference as it have dependency over couple of projects and DLLs . We have to add reference of Web.Data & Web.Core and we also have to add NMock.dll , nunit.core & nunit.framework .In this project we have a class file named MockTest.cs
1 2 3 |
<span style="color: blue">using </span>System.Collections.Generic;<br /><span style="color: blue">using </span>NMock2;<br /><span style="color: blue">using </span>NUnit.Framework;<br /><span style="color: blue">using </span>Web.Data;<br /><span style="color: blue">using </span>Web.Core;<br /><br /><span style="color: blue">namespace </span>BL.Test<br />{<br /> [<span style="color: #2b91af">TestFixture</span>]<br /> <span style="color: blue">public class </span><span style="color: #2b91af">MockTest<br /> </span>{<br /> <span style="color: green">/* Assign property of our object that will be executed.<br /> */<br /> </span><span style="color: blue">public </span><span style="color: #2b91af">Mockery </span>_mock;<br /> <span style="color: blue">public </span><span style="color: #2b91af">IMockDAL </span>_mockDAL;<br /> <span style="color: blue">public </span><span style="color: #2b91af">IEmployeeService </span>_employeeService; <br /><br /> [<span style="color: #2b91af">SetUp</span>]<br /> <span style="color: blue">public void </span>init()<br /> {<br /> <span style="color: green">/* initializing our objects.<br /> */<br /> </span>_mock = <span style="color: blue">new </span><span style="color: #2b91af">Mockery</span>();<br /> _mockDAL = _mock.NewMock<<span style="color: #2b91af">IMockDAL</span>>();<br /> _employeeService = <span style="color: blue">new </span><span style="color: #2b91af">EmployeeService</span>();<br /> _employeeService.MockDAL = _mockDAL;<br /><br /> }<br /><br /> [<span style="color: #2b91af">Test</span>]<br /> <span style="color: blue">public void </span>TestEmployeeCount()<br /> {<br /> <span style="color: green">/* In the list bellow we are creating our virtual list of<br /> * string, which we are expecting after calling the DAL<br /> * this list will be returned.<br /> */<br /> </span><span style="color: #2b91af">List</span><<span style="color: blue">string</span>> employeeList = <span style="color: blue">new </span><span style="color: #2b91af">List</span><<span style="color: blue">string</span>>();<br /> <span style="color: blue">string </span>emp = <span style="color: #a31515">"Maix"</span>;<br /> <span style="color: blue">string </span>emp2 = <span style="color: #a31515">"Pain"</span>;<br /> employeeList.Add(emp);<br /> employeeList.Add(emp2);<br /><br /><br /> <span style="color: green">/* _mockDAL -> is our mocked DAL object.<br /> * GetNameListDAL -> is one of the method name of the _mockDAL object.<br /> * With(true) -> the single boolean parameter of the GetNameListDAL method.<br /> * employeeList -> The expecter return value from the GetNameListDAL method.<br /> */<br /> </span><span style="color: #2b91af">Expect</span>.Once.On(_mockDAL).Method(<span style="color: #a31515">"GetNameListDAL"</span>).With(<span style="color: blue">true</span>).Will( <span style="color: #2b91af">Return</span>.Value(employeeList));<br /><br /> <span style="color: #2b91af">List</span><<span style="color: blue">string</span>> employeeListResult = _employeeService.GetAllEmployee();<br /><br /> <span style="color: green">/* Verify the mock object is executed successfully with its expectation.<br /> */<br /> </span>_mock.VerifyAllExpectationsHaveBeenMet();<br /><br /> <span style="color: #2b91af">Assert</span>.AreNotEqual(<span style="color: blue">null</span>, employeeListResult);<br /> }<br /> }<br />} |
To make it run we can set the project as our startup project and configure the NUnit .
That’s all for the day.
BYE