1

我正在重写这个问题,以更清楚地说明我需要做什么。我正在尝试使用 Rhino-Mock 进行测试:

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

这是代码:

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在上下文的一个属性中,我希望它在调用代码时IObjectSet返回一个空的IQueryable<TxRxMode> 对象。return m_context.TxRxModes.Where(txRxMode => txRxMode.Active);

当我运行它时,Expect 方法调用会抛出 ArgumentNullException:

值不能为空。参数名称:谓词

我试过更简单的:

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

但这会在我调用时引发空引用异常

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

基本上,这是我试图模拟的方法的一部分,所以关键问题是如何模拟这个 Where 语句?

4

1 回答 1

1

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.

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).

于 2010-02-01T02:52:19.047 回答