我正在尝试为新的 mvc3 项目做一些基本的概念验证类型代码。我们将 Moq 与 RavenDB 一起使用。
行动:
public ActionResult Index(string id)
{
var model = DocumentSession.Query<FinancialTransaction>()
.Where(f => f.ResponsibleBusinessId == id);
return View(model);
}
测试:
private readonly Fixture _fixture = new Fixture();
[Test]
public void Index_Action_Returns_List_Of_FinancialTransactions_For_Business([Random(0, 50, 5)]int numberOfTransactionsToCreate)
{
// Arrange
var session = new Mock<IDocumentSession>();
var financialController = new FinancialController { DocumentSession = session.Object };
var businessId = _fixture.CreateAnonymous<string>();
var transactions = _fixture.Build<FinancialTransaction>()
.With(f => f.ResponsibleBusinessId, businessId)
.CreateMany(numberOfTransactionsToCreate);
// Mock
var ravenQueryableMock = new Mock<IRavenQueryable<FinancialTransaction>>();
ravenQueryableMock.Setup(x => x.GetEnumerator()).Returns(transactions.GetEnumerator);
ravenQueryableMock.Setup(x => x.Customize(It.IsAny<Action<Object>>()).GetEnumerator()).Returns(() => transactions.GetEnumerator());
session.Setup(s => s.Query<FinancialTransaction>()).Returns(ravenQueryableMock.Object).Verifiable();
// Act
var actual = financialController.Index(businessId) as ViewResult;
// Assert
Assert.IsNotNull(actual);
Assert.That(actual.Model, Is.InstanceOf<List<FinancialTransaction>>());
var result = actual.Model as List<FinancialTransaction>;
Assert.That(result.Count, Is.EqualTo(numberOfTransactionsToCreate));
session.VerifyAll();
}
看来问题出在 .Where(f => f.ResponsibleBusinessId == id) 中。从模拟的 IRavenQueryable 中,我返回了一个 FinancialTransactions 列表,所以有人会认为 .Where() 会根据它进行过滤。但由于它是 IQueryable,我猜它在枚举时试图将表达式全部作为一个执行。
为了验证,我将操作的查询更改为:
var model = DocumentSession.Query<FinancialTransaction>()
.ToList()
.Where(f => f.ResponsibleBusinessId == id);
这确实让测试通过,但是,它并不理想,因为这意味着它将枚举所有记录,然后过滤它们。
有什么方法可以让 Moq 使用它吗?