Mock unit testing
Mock unit testing is very useful for developer, but my point of view its fake unit testing, its making us foolish.
Lets see how it is...
I have created on console application in visual studio and try to explain two cases as below...
Case1
Case1: I have created Case1 folder in "UnitTestCaseProject" and write below two classes.namespace UnitTestCaseProject.Case1
{
public class checkEmployee
{
public virtual Boolean checkEmp()
{
throw new NotImplementedException();
//return true;
}
}
}
namespace UnitTestCaseProject.Case1
{
public class processEmployee
{
public bool insertEmployee(checkEmployee objtmp)
{
objtmp.checkEmp();
return true;
}
}
}
Case2
I have created Case2 folder in "UnitTestCaseProject" and write below three classes.namespace UnitTestCaseProject.Case2
{
public interface IGetDataRepository
{
string GetNameById(int id);
}
}
namespace UnitTestCaseProject.Case2
{
public class EmployeeRepository : IGetDataRepository
{
public string GetNameById(int id)
{
string name;
if (id == 1)
{
name = "Excellent";
}
else if (id == 2)
{
name = "Expert";
}
else
{
name = "Not Found";
}
return name;
}
}
}
namespace UnitTestCaseProject.Case2
{
public class Implementation1
{
private readonly IGetDataRepository _data;
public Implementation1(IGetDataRepository data)
{
_data = data;
}
public string GetNameById(int id)
{
return _data.GetNameById(id);
}
}
}
Test Project
Now, I have added new test project in solution with name "UnitTestCaseProject.Test" and write unit test cases for above two cases.I have installed "Moq" packages using manage nuget packages...
namespace UnitTestCaseProject.Test
{
[TestClass]
public class Case1Test
{
[TestMethod]
public void insertEmployeeTestSuccess()
{
Mock<checkEmployee> chk = new Mock<checkEmployee>();
chk.Setup(x => x.checkEmp()).Returns(true);
processEmployee obje = new processEmployee();
Assert.AreEqual(obje.insertEmployee(chk.Object), true);
}
}
}
namespace UnitTestCaseProject.Test
{
[TestClass]
public class Cast2Test
{
[TestMethod]
public void TestMethod2()
{
var mock = new Mock<IGetDataRepository>();
mock.Setup(p => p.GetNameById(1)).Returns("demo");
Implementation1 home = new Implementation1(mock.Object);
string result = home.GetNameById(1);
Assert.AreEqual("demo", result);
}
}
}
While I run the above two unit test cases, it gives me success result.
Real Implementation and usage
Now, below code is actual implementation in our project "UnitTestCaseProject".namespace UnitTestCaseProject
{
class Program
{
static void Main(string[] args)
{
processEmployee processEmployee = new processEmployee();
checkEmployee checkEmployee = new checkEmployee();
var result = processEmployee.insertEmployee(checkEmployee);
Console.WriteLine(result);
}
}
}
Now, if I run the project it gives me an error in actual usage, that should not give me any error while I already tested with unit test cases. So, you can see that it is not useful in our real project or application.
I suggest while you deploy project or application in Live environment, then we should write test cases with test environment.
Hope this will help you and save your time.
Enjoy !!!
:)
No comments:
Post a Comment