In the .net 3.5 project that I am working on right now, I was writing some tests for a service class.
public class ServiceClass : IServiceClass
{
private readonly IRepository _repository;
public ServiceClass(IRepository repository)
{
_repository = repository;
}
#region IServiceClass Members
public IEnumerable<ActionType> GetAvailableActions()
{
IQueryable<ActionType> actionTypeQuery = _repository.Query<ActionType>();
return actionTypeQuery.Where(x => x.Name == "debug").AsEnumerable();
}
#endregion
}
and I was having a hard time figuring out how to stub or mock the
actionTypeQuery.Where(x => x.Name == "debug")
part.
Here's what I got so far:
[TestFixture]
public class ServiceClassTester
{
private ServiceClass _service;
private IRepository _repository;
private IQueryable<ActionType> _actionQuery;
[SetUp]
public void SetUp()
{
_repository = MockRepository.GenerateMock<IRepository>();
_service = new ServiceClass(_repository);
}
[Test]
public void heres_a_test()
{
_actionQuery = MockRepository.GenerateStub<IQueryable<ActionType>>();
_repository.Expect(x => x.Query<ActionType>()).Return(_actionQuery);
_actionQuery.Expect(x => x.Where(y => y.Name == "debug")).Return(_actionQuery);
_service.GetAvailableActions();
_repository.VerifyAllExpectations();
_actionQuery.VerifyAllExpectations();
}
}
[Note: class names have been changed to protect the innocent]
But this fails with a System.NullReferenceException
at
_actionQuery.Expect(x => x.Where(y => y.Name == "debug")).Return(_actionQuery);
So my question is:
How do I mock or stub the IQueryable.Where function with RhinoMocks and get this test to pass?
If my current setup won't allow me to mock or stub IQueryable, then give a reasoned explanation why.
Thanks for reading this epically long question.