Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get PEX to automatically generate inputs for code involving LINQ
    primarykey
    data
    text
    <p>I'm having trouble getting PEX to automatically cover methods calling Linq extension methods, such as Where() and Contains() in this example:</p> <pre><code>public class MyEntity { public int Id { get; set; } } public interface IWithQueryable { IQueryable&lt;MyEntity&gt; QueryableSet(); } public class ConsumerOfIhaveIQueryable { private readonly IWithQueryable _withIQueryable; public ConsumerOfIhaveIQueryable(IWithQueryable withIQueryable) { // &lt;pex&gt; Contract.Requires&lt;ArgumentNullException&gt;( withIQueryable != null, "withIQueryable"); // &lt;/pex&gt; _withIQueryable = withIQueryable; } public IEnumerable&lt;MyEntity&gt; GetEntitiesByIds(IEnumerable&lt;int&gt; ids) { Contract.Requires&lt;ArgumentNullException&gt;(ids != null, "ids"); // &lt;pex&gt; Contract.Assert (this._withIQueryable.QueryableSet() != (IQueryable&lt;MyEntity&gt;)null); // &lt;/pex&gt; IEnumerable&lt;MyEntity&gt; entities = _withIQueryable.QueryableSet().Where( entity =&gt; ids.Contains(entity.Id)); if (entities.Count() != ids.Count()) { return null; } return entities; } } [PexClass(typeof(ConsumerOfIhaveIQueryable))] [PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))] [PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)] [TestClass] public partial class ConsumerOfIhaveIQueryableTest { [PexMethod] public IEnumerable&lt;MyEntity&gt; GetEntitiesByIds( [PexAssumeUnderTest]ConsumerOfIhaveIQueryable target, int[] ids) { var result = target.GetEntitiesByIds(ids); PexAssert.IsTrue(result.Count() == ids.Length); return result; } } </code></pre> <p>When I'm running PEX exploration on this PexMethod I'm seeing the following problems:</p> <ul> <li>I keep getting the same exception and PEX keeps suggesting the same "invariant" fix in the form of the Contract.Assert that you see in the // region: I believe the problem is somehow related to how Pex relates to Linq but I'm not sure</li> </ul> <p>--- Description failing test: ArgumentNullException, Value cannot be null. Parameter name: source</p> <pre><code>[TestMethod] [PexGeneratedBy(typeof(ConsumerOfIhaveIQueryableTest))] [PexRaisedException(typeof(ArgumentNullException))] public void GetEntitiesByIdsThrowsArgumentNullException385() { using (PexChooseBehavedBehavior.Setup()) { SIWithQueryable sIWithQueryable; ConsumerOfIhaveIQueryable consumerOfIhaveIQueryable; IEnumerable&lt;MyEntity&gt; iEnumerable; sIWithQueryable = new SIWithQueryable(); consumerOfIhaveIQueryable = ConsumerOfIhaveIQueryableFactory.Create((IWithQueryable)sIWithQueryable); int[] ints = new int[0]; iEnumerable = this.GetEntitiesByIds(consumerOfIhaveIQueryable, ints); } } </code></pre> <p>--- Exception details</p> <p>System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.IQueryable'1 System.Linq.Queryable.Where(System.Linq.IQueryable'1 source, System.Linq.Expressions.Expression'1> predicate) c:\users\moran\documents\visual studio 2010\Projects\PexTuts\PexIQueryable\PexIQueryable\ConsumerOfIhaveIQueryable.cs(29): at System.Collections.Generic.IEnumerable'1 PexIQueryable.ConsumerOfIhaveIQueryable.GetEntitiesByIds(System.Collections.Generic.IEnumerable`1 ids) c:\users\moran\documents\visual studio 2010\Projects\PexTuts\PexIQueryable\PexIQueryable.Tests\ConsumerOfIhaveIQueryableTest.cs(34): at System.Collections.Generic.IEnumerable'1 PexIQueryable.ConsumerOfIhaveIQueryableTest.GetEntitiesByIds(PexIQueryable.ConsumerOfIhaveIQueryable target, System.Int32[] ids) </p> <ul> <li>I can't get PEX to generate relevant inputs. As you can see, I tried to "help" it by adding a PexAssert and a branch in my code, but this branch is never covered, even though it <em>should</em> be relatively simple to generate a code that would walk that path. PEX only tries to pass null or an empty array as the list of Ids (I read somewhere that it is easier for PEX to work with arrays (int[]) rather than IEnumerable.</li> </ul> <p>Would love to get some comments on this...</p> <p>BTW, this is my first SO post, Hope I didn't spam with too much code and info.</p> <p>Moran</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload