简体   繁体   English

如何在EF4中模拟Where子句

[英]How do I mock a Where clause in EF4

I am re-writing this question to make it clearer what I need to do. 我正在重新编写此问题,以使我需要做的事情更加清楚。 I am trying to use Rhino-Mock to test: 我正在尝试使用Rhino-Mock进行测试:

    public IQueryable<TxRxMode> GetAllModes()
    {
        return m_context.TxRxModes.Where(txRxMode => txRxMode.Active);
    }

Here's the code: 这是代码:

var context = MockRepository.GenerateStub<IProjectContext>();

//Returns an empty list
context.Expect(c => c.TxRxModes.Where(Arg<Func<TxRxMode, bool>>.Is.Anything)).Return(new List<TxRxMode>().AsQueryable());

TxRxModes in an IObjectSet property on the context and I want it to return an empty IQueryable<TxRxMode > object when the return m_context.TxRxModes.Where(txRxMode => txRxMode.Active); TxRxModes在上下文的IObjectSet属性中,并且我希望它在return m_context.TxRxModes.Where(txRxMode => txRxMode.Active);时返回一个空的IQueryable<TxRxMode >对象return m_context.TxRxModes.Where(txRxMode => txRxMode.Active); code is called. 代码被调用。

When I run this, the Expect method call throws the an ArgumentNullException: 当我运行此代码时,Expect方法调用将引发ArgumentNullException:

Value cannot be null. 值不能为空。 Parameter name: predicate 参数名称:谓词

I have tried the simpler: 我尝试了更简单的方法:

IObjectSet<TxRxMode> modes = MockRepository.GenerateStub<IObjectSet<TxRxMode>>();
context.Expect(c => c.TxRxModes).Return(modes);

but this throws a null reference exception when I call 但这会在我调用时引发空引用异常

return m_context.TxRxModes.Where(txRxMode => txRxMode.Active);

Basically, this is part of the method I am trying to mock, so the key question is how do I mock this Where statement? 基本上,这是我尝试模拟的方法的一部分,所以关键的问题是如何模拟此Where语句?

Where is actually a global static method and you shouldn't be mocking it. 全局静态方法实际上在哪里,您不应该嘲笑它。 It operates on an IEnumerable however and you could just mock that. 它在IEnumerable上运行,但是您可以对其进行模拟。

Its kind of a hassle doing it with rhino mocks however. 但是,使用犀牛模拟游戏有点麻烦。 I would recommend doing the mock manually (if you need to do it at all). 我建议手动进行模拟(如果需要的话)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM