.Net Core : Unit Test with NUnit
Below example is implemented with Nunit test to test .net core project feature.
Create test class,
namespace DotNetCoreDemo.Test
{
public class EmployeeServiceTest
{
}
}
OneTimeSetUp
IEmployeeService employeeService;
public Mock<IEmployeeRepository> mockEmployeeRepository;
[OneTimeSetUp]
public void Init()
{
mockEmployeeRepository = new Mock<IEmployeeRepository>();
employeeService = new EmployeeService( this.mockEmployeeRepository.Object);
}
static object[] employeeSource =
{
new object[]
{
new Employee[]
{
new Employee()
{
Id = 1, Name = "demo1", Email = "demo1@demo.com"
},
new Employee()
{
Id = 2, Name = "demo2", Email = "demo2@demo.com"
}
}
}
};
static object[] employeeSourceNull =
{
new object[]
{
new Employee[]
{
}
}
};
Test for get data,
[Test, TestCaseSource("employeeSource")]
public void GetEmployeeList_Return_True(Employee[] employees)
{
//Init
mockEmployeeRepository.Setup(p => p.GetAllEmployee()).Returns(employees.AsQueryable().ToList());
//Act
List<Employee> result = employeeService.GetAllEmployee().ToList();
////Assert
Assert.Greater(result.Count, 0);
}
Test for no data,
[Test, TestCaseSource("employeeSourceNull")]
public void GetEmployeeList_NoData_Return_True(Employee[] employees)
{
//Init
mockEmployeeRepository.Setup(p => p.GetAllEmployee()).Returns(employees.AsQueryable().ToList());
//Act
List<Employee> result = employeeService.GetAllEmployee().ToList();
////Assert
Assert.AreEqual(result.Count, 0);
}
Hope this will help you and save your time.
Enjoy !!!
:)
No comments:
Post a Comment