score:1
the short answer is: no, because .where() and .select() are extension methods, which cannot be mocked.
the longer answer is: yes, because .where()
and .select()
on iqueryable<>
s do nothing but indicate to the underlying query provider that they were just called. so you could technically create a stub for the query provider and look at what happened to it before it got evaluated.
but the easy answer is: i've found the best approach is to use an actual in-memory representation that is capable of acting like a queryable, like a list. then, rather than trying to validate the lambda expressions themselves, test the resulting data.
var options = new[] {new option(...)};
repositorymock.setup(r => r.context).returns(contextmock.object);
contextmock.setup(c => c.asqueryable<option>()).returns(options.asqueryable());
...
assert.areequal(results[0], options[0].id);
the downside to this is that there's no way to test that your method only uses expressions that can be translated by your query provider. but i generally find this is "good enough" for unit-testing purposes.
score:2
as an option, use your code as a dependency. this way you can stub it without touching context at all. for example:
public class optionservice : ioptionservice
{
private irepository _repository;
public optionservice(irepository repository)
{
_repository = repository;
}
public int[] getoptionswithname(int id, string name)
{
_repository.context.asqueryable<option>()
.where(o => o.id == id && o.name == name)
.select(o => o.id)
.toarray();
}
}
public interface ioptionservice
{
int[] getoptionswithname(int id, string name);
}
inject ioptionservice
into your code with logic similar, how irepository
is injected into the optionservice
, and stub the method getoptionswithname
in the test to return whatever you want.
Source: stackoverflow.com
Related Query
- Stubbing Code for Test With Linq Expressions and Lambdas
- Saved projection expression for re-use in different linq expressions with two source objects
- Best practices for dealing with LINQ statements that result in empty sequences and the like?
- How linq function OrderByDescending and OrderBy for string length works internally? Is it faster than doing it with loop?
- What are the prerequisites for using SQL and LINQ with my winforms application in C#?
- C# - Linq optimize code with List and Where clause
- Using C# LINQ Expressions for both Value Types and Reference Types
- List Categories and it's count of products with LinQ using Lambda or classic expressions
- SQL Query to LINQ for MVC with SUM and IS NOT NULL
- Linq for calculating rank with group and orderby
- Write Dynamic LINQ queries for sorting and projecting with EF Core
- Code Rewite for tuple and if else statements by using LINQ
- Linq to SQL using Lambda expressions together with Join, GroupBy, Count and Sum
- Implement Between and other functions as Expressions for LINQ
- Cannot build the Test project for LINQ IQueryable Toolkit (IQToolkit) - Code 9009
- LINQ to SQL Classes with INotifyPropertyChanged and IDataErrorInfo for WPF bindings
- Lost with LINQ and Expressions
- source code for LINQ 101 samples
- LINQ: Help with linq query and contains for an IEnumerable<string>?
- LINQ syntax for SQL query with multiple inner joins and aliases
- Using LINQ and Reflection: How to query for all Classes with [Authorize] Attribute in my Assembly?
- How To correctly set Parameters for Function with LinQ and List<T>?
- Searching for multiple strings using single database query with entity framework and LINQ
- LINQ with AND or OR for 2 or more enumerable lists
- C# LINQ code for two list compare and replace
- Mock same method in a test using Moq with different linq expressions
- How to query two tables for matching values with linq and C#?
- How to sum a field for all records and then return the max of all summed records with Linq
- Optimized code for Insert and Update in one method with LINQ?
- Trouble generating c# linq to entities (EF) query for a 3 table join with or and one table with no key relationships
More Query from same tag
- What is an efficient way of iterating through a list according to its distinct values?
- LINQ with string variable for sorting
- Select the Most Recent Entry of Each Distinct Value in Linq
- Grab a portion of List<string>
- Search contains in a string column for a list of strings using LINQ
- LINQ to XML - parent's attribute in where clause
- How to use LINQ to set null values last in ordered list of integers
- get last record of a table in database?
- Why is linq slow in c#
- How to optimize this linq query by getting two numbers in one call?
- LINQ Order by Error
- How do I save a reordered list back to the SQL Server?
- How might I clean up this lambda?
- LINQ problem: FK-Table not set correctly
- How to write the SQL ORDER BY {value} DESC LIMIT statement in LINQ to Entities (Entity Framewok)?
- How to read xml file having different hierarchy in .net
- how to populate navigational properties when using Entity Framework
- Many outer join with SQL to LINQ
- How to get datarow array refering to datatable 's field value with linq
- Cannot see FirstOrDefault
- Constructing a linq query to get associations
- Order dynamic list of object by common property with pagination
- How can I do an inline sort?
- join diffrent field type in linq
- Query with Nest field boosting returning no results from Elasticsearch
- Entity Framework Many-To-Many + Count
- Data comparing in dataset
- Weird exceptions compiled dynamically built expression
- Update a List of KeyValue pair
- Select all records from 2 tables - many-to-many EF C#