Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries Faking LINQ Queries

Faking LINQ Queries

You can fake the result of a LINQ query.

When to Use

When your test requires to fake a SQL query or query result.

Syntax

C#

Isolate.WhenCalled(()=><linq_query>.WillReturnCollectionValuesOf(fakedList.AsQueryable());

VB

Isolate.WhenCalled(Function() <linq_query>).WillReturnCollectionValuesOf(fakedList.AsQueryable())

Samples

Sample 1: Faking the Result of a LINQ Query

The following sample shows how to fake the result of a LINQ query.

C#

public void ProcessCustomerList()
{
  var nycCustomers = from customer in customerList.USCustomers where customer.City == "NYC" select customer;
  // Process list
}

//The following sample shows how to fake the entire query and return your own customer list instead.
var fakedList = new List<USCustomers>();
Isolate.WhenCalled(()=> from customer in customerList.USCustomers where customer.City == "NYC"select customer).WillReturnCollectionValuesOf(fakedList.AsQueryable());

VB

Public Sub ProcessCustomerList()
	Dim nycCustomers = From customer In customerList.USCustomers Where customer.City = "NYC" customer
	' Process list
End Sub

'The following sample shows how to fake the entire query and return your own customer list instead.
Isolate.WhenCalled(Function() From customer In customerList.USCustomers Where customer.City = "NYC" select customer).WillReturnCollectionValuesOf(fakedList.AsQueryable())

Sample 2: Returning a Queryable Fake Result

If the customer list is retrieved from the database, you might want to return a faked set from the USCustomers property.

The following sample shows how to fake the USCustomers property and return it as IQueryable.

C#
var fakedList = new List<USCustomers>();
Isolate.WhenCalled(() => customerList.USCustomers).WillReturnCollectionValuesOf(fakedList.AsQueryable());

VB

Dim fakedList = New List(Of USCustomers)()
Isolate.WhenCalled(Function() customerList.USCustomers).WillReturnCollectionValuesOf(fakedList.AsQueryable())