score:1
after some time setting up the code for this, i've made a few assumptions. i'm assuming that you're stubbing the iwithqueryable via a moles stub and also that the nullargumentexception occurs when you remove the contract assertion that the queryableset() method doesn't return null.
as for the code, imo the more code the better, as long as it's relevant- far better to have too much than too little to go on, so that's fine. as above, do try to make clear all the assumptions in the code (e.g. the moles stubbing (as there's different ways to achieve this and it's something one has to assume).
i'm not 100% sure what you're asking. the code is failing because the stubbed iwithqueryable object
doesn't have an implmementation for the queryableset()
method and that method returns null
. the pexassert
here won't help it figure out how to create a linq provider, which is what you're asking it to do. the pexchoosebehavedbehavior.setup()
simply replaces any calls to delegates on the moles stubs (which don't have a custom delegate) with the default behaviour which is default(t)
, so that's why the source
is null- the queryableset()
is initialised to null
.
you can solve this in a few ways (at least in the sense of providing a way of creating the queryableset()
method). you can create a factory method to generate either the whole siwithqueryable
, or just the queryableset
delegate. this is something that pex suggests (however, with me it got the types and namespaces muddled-up). for instance:
/// <summary>a factory for microsoft.moles.framework.molesdelegates+func`1[system.linq.iqueryable`1[stackoverflow.q9968801.myentity]] instances</summary>
public static partial class molesdelegatesfactory
{
/// <summary>a factory for microsoft.moles.framework.molesdelegates+func`1[system.linq.iqueryable`1[stackoverflow.q9968801.myentity]] instances</summary>
[pexfactorymethod(typeof(molesdelegates.func<iqueryable<myentity>>))]
public static molesdelegates.func<iqueryable<myentity>> createfunc()
{
throw new invalidoperationexception();
// todo: edit factory method of func`1<iqueryable`1<myentity>>
// this method should be able to configure the object in all possible ways.
// add as many parameters as needed,
// and assign their values to each field by using the api.
}
/// <summary>a factory for microsoft.moles.framework.molesdelegates+func`1[system.linq.iqueryable`1[stackoverflow.q9968801.myentity]] instances</summary>
[pexfactorymethod(typeof(siwithqueryable))]
public static siwithqueryable create()
{
var siwithqueryable = new siwithqueryable();
siwithqueryable.queryableset = () => { throw new invalidoperationexception(); };
return siwithqueryable;
// todo: edit factory method of func`1<iqueryable`1<myentity>>
// this method should be able to configure the object in all possible ways.
// add as many parameters as needed,
// and assign their values to each field by using the api.
}
}
and then hook it up to the test method with one of the two lines assigning siwithqueryable
:
[testmethod]
[pexgeneratedby(typeof(consumerofihaveiqueryabletest))]
public void getentitiesbyidsthrowsargumentnullexception678()
{
siwithqueryable siwithqueryable;
// either this for the whole object.
siwithqueryable = molesdelegatesfactory.create();
// or this for just that delegate.
siwithqueryable = new siwithqueryable();
siwithqueryable.queryableset = molesdelegatesfactory.createfunc();
consumerofihaveiqueryable consumerofihaveiqueryable;
ienumerable<myentity> ienumerable;
consumerofihaveiqueryable = consumerofihaveiqueryablefactory.create((iwithqueryable) siwithqueryable);
int[] ints = new int[0];
ienumerable = this.getentitiesbyids(consumerofihaveiqueryable, ints);
}
this will then call your factory methods when creating the stub for iwithqueryable
. this is still a problem, as regenerating the explorations will wipe out the stub setup.
if you provide the parameterless factory method to create the stub (molesdelegatesfactory.createfunc()
), then pex will know about this and generate the tests to use it. so, it will correctly manage the behaviour across test regenerations. unfortuantely, pex suggests creating this delegate as a factory method- however, it is never called, the default implementation is always used, it seems one has to mock the parent type.
however, i'm wondering why you're creating an interface iwithqueryable
that simple wraps another, and also, what you expect to do with the iqueryable
. in order to do anything very useful, you'll have a lot of work going on to deal with the iqueryable
interface - the provider
and expression
mainly, you'll pretty-much have to write a mock query provider, which will not be easy.
Source: stackoverflow.com
Related Query
- How to get PEX to automatically generate inputs for code involving LINQ
- C# LINQ or for loop How to get a data source from a db?
- How can I get LINQ to return the object which has the max value for a given property?
- How to get linq `ForEach` statement to return data on the method call being made for each list object?
- How to get in LINQ one row for each key
- How to reuse a linq expression for 'Where' when using multiple source tables
- linq - how do you do a query for items in one query source that are not in another one?
- How to get SQL query into LINQ form in C# code
- how do you generate class for LINQ to SQL?
- How can I generate all possible LINQ strings of a json object for Json.net?
- C# LINQ How to get a data source from a db?
- How can I refactor this code for LINQ filtering?
- How to get object field for LINQ cached object?
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- how to write LINQ to objects equivalent code for this code
- C# Linq How to enumerate over time periods between two dates to get data for a graph
- source code for LINQ 101 samples
- How to dynamically create linq code at runtime and get results
- How does this LINQ query get initial values for this string?
- How can i get date from foreach for linq
- How do I get all invoices for a customer using the Linq extensions in QuickBooks Online API v3?
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- How to write LINQ for multiple joins involving self join?
- LINQ SUM with Inner Joins: How to get the Sum for a TimeSpan
- How I get the right linq statement for filter to arraylist values in my xml file?
- How to get Linq data context for this function?
- how to get child elements for specific node XML with linq
- Unsure how to generate Linq for my query SQL
- Suggestions on how to optimize code for ordering by string in linq
- How to get first record in each group using Linq
More Query from same tag
- Linq Update Query error in WP8
- Getting Dictionary<string,string> from List<Control>
- conditional Updating a list using LINQ
- How can I divide a collection into sub collections of 50 elements each in C#?
- C# List Comprehensions = Pure Syntactic Sugar?
- join expression to linq query
- How to get continuous characters in C#?
- How to get object from .Min extension method?
- Using external method in linq
- Nothing gets added to Object list
- What is best and rapid way for calculate this query?
- Simple LINQ select (using Entity Framework) does not return values
- "OR " condition in where clause of linq query is returning null
- Creating xml with xdocument
- LinQ method syntax multiple tables with .where() and without joins
- Using LINQ with tree structures
- Entity Framework: Mixed predicates of mixed entities
- Calculation using grouped data in Linq to entities
- How to get last row + column from a collection in C# using lambda
- How to compare with a string array and string
- Marten JasperFx - How to append query expressions?
- Dynamic linq-to-sql that queries based on multiple keywords
- Bulk-deleting in LINQ to Entities
- Check if results from LINQ query contains a value
- Convert Expression<Func<TInterface, bool>> to Expression<Func<TImplementation, bool>>
- XML with reporting services namespaces enumeration yielded no result
- XDocument Error Nonwhitespace
- How to find a string in text
- linq query for set attribute
- Sorting Linq Result by Year and Month