Recently, we've been mocking out IQueryable's as return values, which had led to setups that look like the following...

1   programs.setup_result_for(x => x.All()).Return(new List<IProgram> {active_program,inactive_program}.AsQueryable());

I just switched over to the following syntax... by creating an extension method.

1   programs.setup_result_for(x => x.All()).will_return(active_program,inactive_program);

The following are the extensions methods to make this work.

1   public static IMethodOptions<IEnumerable<R>> will_return<R>(this IMethodOptions<IEnumerable<R>> options, params R[] items)
2   {
3       return options.Return(items);
4   }
5 
6   public static IMethodOptions<IQueryable<R>> will_return<R>(this IMethodOptions<IQueryable<R>> options, params R[] items)
7   {
8       return options.Return(new Query<R>(items));
9   }

and...

 1   internal classQuery<T> : IQueryable<T> 
 2   { 
 3      private readonly IQueryable<T> query; 
 4   
 5    public Query(params T[] items) 
 6    { 
 7      query = items.AsQueryable(); 
 8    } 
 9   
10    public Expression Expression 
11    { 
12      get { return query.Expression; } 
13    } 
14   
15    public Type ElementType 
16    { 
17      get { return query.ElementType; } 
18    } 
19   
20    public IQueryProvider Provider 
21    { 
22      get { return query.Provider; } 
23    } 
24   
25    public IEnumerator<T> GetEnumerator() 
26    { 
27      return query.GetEnumerator(); 
28    } 
29   
30    IEnumerator IEnumerable.GetEnumerator() 
31    { 
32      return GetEnumerator(); 
33    } 
34   }

Hope, this helps!

comments powered by Disqus