如何从单元测试中覆盖Linq语句?

时间:2015-05-18 10:31:50

标签: unit-testing moq rhino-mocks unity-test-framework

我有UpdateEmployees()方法更新所有员工数据。

在为该方法编写单元测试时,它不会覆盖OrderBy()语句。如何获得order by语句的代码覆盖率。

我使用过Moq单元测试。

//This Method returns list of employee
Public void UpdateEmployees()
{
     var employees = _repository.GetAllEmployees().OrderBy(x => x.Name);
     // Some Update operations below.
}

//Unit Test for GetAllEmployee Method.
Public void UnitTest()
{
    var employees = new Fixture.Build<Employee>().Createmany(5).ToList();
    var moqRepository = new Mock<IRepository>(MockBehavior.Strict);
    moqRepository.Setup(r => r.GetAllEmployees()).Returns(employees)

    var employee = new Employee(moqRepositort.Object);
    employee.UpdateEmployees();
}

1 个答案:

答案 0 :(得分:0)

如果将List枚举为List:

,它将被覆盖
var employees = _repository.GetAllEmployees().OrderBy(x => x.Name).ToList();