1

使用 c#、Moq、MSTest、LinqToExcel

我无法找出 Setup() 的最佳方法来模拟 Linq-to-Excel IExcelQueryFactory 上的 Linq 查询的响应。

我想我应该期望 Linq 查询返回委托类型的东西,但我不太确定它应该是什么样子

谁能建议我的 Return() 在下面的 Moq Setup() 中应该是什么样子?

另外,对我测试和模拟这些 Lynq 方法的方法有任何疑问吗?我应该以不同的方式处理这个问题吗?

谢谢!我现在要去预订代表。:)

考试

[TestClass]
public class ThingsSheetTests
{
    [TestMethod]
    public void GetRows_ReturnsListOfThings()
    {
        // Arrange
        var mockExcelQueryFactory = new Mock<IExcelQueryFactory>();
        var thingsSheet = new ThingsSheet(mockExcelQueryFactory.Object, "file", "worksheet");

        mockExcelQueryFactory
        .Setup(x => x.Worksheet<Thing>(It.IsAny<string>))

        // I think this is correctly casting to a delegate
        // however ExelQuerable constructor needs arguments:
        // public ExcelQueryable(IQueryProvider provider, Expression expression);
        // looking into what kind of IQueryProvider and Expression I should supply.

        .Returns(Action(() => new ExcelQueryable<Thing> { }));   // getting closer!


        // Act
        thingsSheet.GetRows();

        // Assert
        mockExcelQueryFactory.Verify();

    }
}

我正在测试的类和方法

public class ThingsSheet
{
    private string importFile;
    private string worksheetName;
    private IExcelQueryFactory excelQueryFactory;
    public ThingsSheet(IExcelQueryFactory excelQueryFactory, string importFile, string worksheetName)
    {
        this.excelQueryFactory = excelQueryFactory;
        this.importFile = importFile;
        this.worksheetName = worksheetName;
        this.AddMappings();
    }

    private void AddMappings()
    {
        excelQueryFactory.AddMapping<Thing>(t => t.Id, "Thing ID");
    }

    public List<Thing> GetRows()
    {
        excelQueryFactory.AddMapping<Thing>(t => t.Id, "Thing ID");

        var things = from thing in excelQueryFactory.Worksheet<Thing>(this.worksheetName)
                   select new Thing { };

        return things.ToList<Thing>();
    }
}
4

1 回答 1

1

您可以使用例如返回您的假数据的方法。

mockExcelQueryFactory
    .Setup(x => x.Worksheet<Thing>(It.IsAny<string>()))
    .Returns(ExcelQueryableOfThing()); 

假设Thing 类看起来像这样:

public class Thing
{
    public string Id { get; set; }
    public string Name { get; set; }
}

然后在方法中ExcelQueryableOfThing()你必须模拟CreateQuery<TElement>(Expression expression)IQueryProvider 提供者的方法。像这样的东西:

private ExcelQueryable<Thing> ExcelQueryableOfThing()
{
    var things = new List<Thing>
    {
        new Thing
        {
            Id = "1",
            Name = "Adam"
        },
        new Thing
        {
            Id = "1",
            Name = "Eva"
        }
    }
    .AsQueryable();

    Mock<IQueryProvider> queryProvider = new Mock<IQueryProvider>();
    queryProvider
        .Setup(p => p.CreateQuery<Thing>(It.IsAny<Expression>()))
        .Returns(() => things);

    Expression expressionFake = Expression.Constant(new List<Thing>().AsQueryable());

    return new ExcelQueryable<Thing>(queryProvider.Object, expressionFake);
}

然后在单元测试thingsSheet.GetRows()中将返回您的假数据(Adam 和 Eva :)。高温高压

[TestMethod]
public void GetRows_ReturnsListOfThings()
{
    // Arrange
    Mock<IExcelQueryFactory> mockExcelFile = new Mock<IExcelQueryFactory>();
    var thingsSheet = new ThingsSheet(mockExcelFile.Object, "file", "worksheet");

    mockExcelFile
        .Setup(x => x.Worksheet<Thing>(It.IsAny<string>()))
        .Returns(ExcelQueryableOfThing());

    // Act
    List<Thing> rows = thingsSheet.GetRows();

    // Assert
    Assert.AreEqual(2, rows.Count); // Adam and Eva
}
于 2014-10-13T13:59:53.067 回答