score:1

Accepted answer

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.


Related Query