views:

128

answers:

1

I am re-writing this question to make it clearer what I need to do. I am trying to use Rhino-Mock to test:

    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); code is called.

When I run this, the Expect method call throws the an 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?

+1  A: 

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

George Mauer
Good point about the static, I'd missed that. I have tried to manually mock the IObjectSet interface and now need to provide an IQueryProvider object. More digging needed!
Colin Desmond