【问题标题】:Mocking DbSet.toList模拟 DbSet.toList
【发布时间】:2013-11-15 21:05:35
【问题描述】:

我想模拟DbSet.toList 方法,但我总是遇到异常,因为toList 方法是在IEnumerable 中声明的。

测试方法 SocialSWT.Tests.Repository.RepositoryTest.GetCallsListOfEntities 抛出异常: System.NotSupportedException:表达式引用了不属于模拟对象的方法:foo => foo.ToList()

dbSet.Setup(foo => foo.ToList()).Returns(mockList);

【问题讨论】:

  • ToList 是一种扩展方法。您必须检查您的模拟框架如何支持模拟该类型的方法。
  • 你可能需要模拟 DbSet,我想...

标签: c# testing moq


【解决方案1】:

使用 Moq,没有简单的方法来模拟扩展方法,即 ToList()。您需要抽象出 DbSet,即 IDbSet 和存根。请参阅this SO 问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 2015-01-08
    • 1970-01-01
    • 2019-08-18
    • 2017-01-20
    • 2011-02-15
    • 1970-01-01
    相关资源
    最近更新 更多