0

You are writing unit tests and you want to insatiate an IQueryable() interface. How exactly?

Liran Barniv
  • 1,320
  • 1
  • 10
  • 10
  • Duplicate of [Instantiate empty IQueryable for use with Linq to sql](https://stackoverflow.com/questions/14993048/instantiate-empty-iqueryable-for-use-with-linq-to-sql), [Enumerable.Empty() equivalent for IQueryable](https://stackoverflow.com/q/2691392/8967612) – 41686d6564 stands w. Palestine Sep 17 '22 at 16:39

2 Answers2

1

One straight forward way would be creating object implementing interface, or to use AsQueryable() method on IEnumerable, as suggested in other answer.

One more way, as generally is done with interfaces, is to use mocks, like

var queryableMock = new Mock<IQueryable>();
// Setup mock with queryableMock.Setup method,
// now you can use queryableMock.Object
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0

To instantiate an empty object use the following code where T is your entity type

using System.Linq;

var query = Enumerable.Empty<T>().AsQueryable();

To instantiate an object with a list of entries:

var query = (new List<T> { new T()}).AsQueryable();
Liran Barniv
  • 1,320
  • 1
  • 10
  • 10