Update
While RhinoMocks supports mocking multiple interfaces with the same mock, as I utilized in my initial answer, it apparently doesn't support explicit intefaces, which DbSet
utilizes for IQueryable
. As @Dosihris pointed out in the comment, it throws an exception saying
Due to how CLR handles explicit interface implementation, we're not able to proxy this method.
This appears to be an exception from the version of Castle.DynamicProxy (v2.1.0.5967) embedded in RhinoMocks, which is from pre-2010. Newer versions of DynamicProxy support this, so if you really wanted to be adventurous, you could recompile RhinoMocks with an updated version of DynamicProxy.
Or, you could try referencing DynamicProxy directly in your test project and using binding redirects to force RhinoMocks to use the newer version. Given the age of the version in RhinoMocks, there's likely incompatibilities with the newer DynamicProxy APIs, so this is likely not going to be successful.
Or, you could move to a framework that is still actively maintained, such as Moq or NSubstitute (which both use newer versions of DynamicProxy).
This is the equivalent logic using RhinoMocks:
using Rhino.Mocks;
private static DbSet<T> GetQueryableMockDbSet<T>(params T[] sourceList) where T : class
{
var queryable = sourceList.AsQueryable();
var dbSet = Rhino.Mocks.MockRepository.GenerateMock<DbSet<T>, IQueryable<T>>();
((IQueryable<T>)dbSet).Expect(m => m.Provider).Return(queryable.Provider);
((IQueryable<T>)dbSet).Expect(m => m.Expression).Return(queryable.Expression);
((IQueryable<T>)dbSet).Expect(m => m.ElementType).Return(queryable.ElementType);
((IQueryable<T>)dbSet).Expect(m => m.GetEnumerator())
.Return(null) // will be ignored but still the API requires it
.WhenCalled((methodInvokation) => methodInvokation.ReturnValue = queryable.GetEnumerator());
return dbSet;
}
Depending on whether you want to do verification that the properties were called or not, you may want to use .Stub()
instead of .Expect()
in each of the above.