0

My original search was "mocking dbsets does not return all entries" and I found an entry that appeared to describe my problem.

DbSet mock, no results while calling ToList secondly

However, the solution uses the following syntax

q.Setup(m => m.GetEnumerator()).Returns( () => queryableData.GetEnumerator() );

which gives me this compiler error "Cannot convert lambda expression to type 'System.Collections.Generic.IEnumerator' because it is not a delegate type"

Other searches suggest I add using statements which I already have:

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data;

Here's my method (I'm using RhinoMocks and the other poster was using Moq but I don't think that's my problem)

private static IDbSet<T> GetDbSetTestDouble<T>(List<T> data) where T : class
    {
        IQueryable<T> queryable = data.AsQueryable();

        IDbSet<T> dbSet = MockRepository.GenerateMock<IDbSet<T>, IQueryable>();

        dbSet.Stub(m => m.Provider).Return(queryable.Provider);
        dbSet.Stub(m => m.Expression).Return(queryable.Expression);
        dbSet.Stub(m => m.ElementType).Return(queryable.ElementType);

        // Cannot convert lambda expression to type 'System.Collections.Generic.IEnumerator<T>' because it is not a delegate type
        dbSet.Stub(m => m.GetEnumerator()).Return( () => queryable.GetEnumerator());
    }
Community
  • 1
  • 1
aknowles
  • 21
  • 1
  • I think you simply need to remove the lambda portion (e.g. the `() =>`) and just return tell it to return the result of `queryableData.GetEnumerator()`. – CodingGorilla Mar 14 '16 at 17:14

2 Answers2

3

As the error is trying to tell you, .Return takes a value to return (in this case, an IEnumerator), not a lambda.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

You have to use Generic Enumeration here. Please refer to the below link.

EF6 Mocking derived DbSets

Community
  • 1
  • 1
Thanigainathan
  • 1,505
  • 14
  • 25