0

I have the following method to test :

public float coverageJump(bool a)
{
    int c = 0;
first:
    c++;
    Random random = new Random();
    float result = random.Next(0, 100);
    Console.WriteLine(result);

    if(result < 50)
        goto first;
    if (result == 50)
        goto end;

    if (a)
        goto end;
    else
        goto first;

end:
    return c;
}

Pex suggest me to use Random.Next moled and its create :

PexMethod :

[PexMethod]
public float coverageJump([PexAssumeUnderTest]ClassMethod target, bool a)
{
    float result = target.coverageJump(a);
    return result;
    // TODO: add assertions to method ClassMethodTest.coverageJump(ClassMethod, Boolean)
}

Parametrized Unit Test

[TestMethod]
[PexGeneratedBy(typeof(ClassMethodTest))]
[PexRaisedException(typeof(NullReferenceException))]
[HostType("Moles")]
public void coverageJumpThrowsNullReferenceException489()
{
    float f;
    RandomPreparation.Prepare();
    ClassMethod s0 = new ClassMethod();
    f = this.coverageJump(s0, false);
}

And the prepare method to mock Random Class:

[PexPreparationMethod(typeof(Random))]
public static void Prepare()
{
    MRandom.BehaveAsCurrent();
}

I developed the prepare method to mock Random class

MRandom.BehaveAsCurrent();
MRandom mr = new MRandom()
{
    NextInt32Int32 = (b, c) => { return 1; },
    Sample = () => { return 1; },
    InternalSample = () => { return 1; }
};

MRandom.Constructor = (a) => 
{
    // a.Next = (b, c) => { return 1; };
};

MRandom.Behavior = mr.InstanceBehavior;

But I get the following NULL Exception :

--- Description
failing test: NullReferenceException, Riferimento a un oggetto non impostato su un'istanza di oggetto.

float f;
RandomPreparation.Prepare();
ClassMethod s0 = new ClassMethod();
f = this.coverageJump(s0, false);


[TestMethod]
[PexGeneratedBy(typeof(ClassMethodTest))]
[PexRaisedException(typeof(NullReferenceException))]
[HostType("Moles")]
public void coverageJumpThrowsNullReferenceException387()
{
    float f;
    RandomPreparation.Prepare();
    ClassMethod s0 = new ClassMethod();
    f = this.coverageJump(s0, false);
}

Exception details

System.NullReferenceException: Riferimento a un oggetto non impostato su un'istanza di oggetto.      at System.Int32 System.Random.InternalSample() 
      at System.Double System.Random.Sample() 
      at System.Int32 System.Random.Next(System.Int32 minValue, System.Int32 maxValue) 
    D:\Sviluppo\UNI\TesiTirocinio\src\TutorialsMolePex\BenchMarkTesterTool\BenchMarkTesterToolLib\ClassMethod.cs(154): at System.Single BenchMarkTesterToolLib.ClassMethod.coverageJump(System.Boolean a) 
    D:\Sviluppo\UNI\TesiTirocinio\src\TutorialsMolePex\BenchMarkTesterTool\BenchMarkTesterToolLib.Tests\ClassMethodTest.cs(27): at System.Single BenchMarkTesterToolLib.ClassMethodTest.coverageJump(BenchMarkTesterToolLib.ClassMethod target, System.Boolean a) 

Can anyone help with this?

pickypg
  • 22,034
  • 5
  • 72
  • 84
Boymix81
  • 31
  • 4
  • What's the actual question? And is this homework? – pickypg Sep 15 '12 at 17:26
  • Hi Pickypg , I want to do a thesis about Pex and Moles, so I'm studing the behavior of pex . The quesion is : "How can I mole Rando class to pass always the "1" when the method Random.Next is called by the Unit Test ?" Thanks a lots, Best regards. – Boymix81 Sep 15 '12 at 18:38
  • You should look at Microsoft's Fakes instead of Moles. Moles is deprecated, and Fakes is its substitution (by the same team at MS Research, by the way). http://msdn.microsoft.com/en-us/library/hh549175.aspx – Fabio Sep 25 '12 at 02:13

1 Answers1

0

I find this solution to my problem in the Prepare Method:

Prepare():

    [PexPreparationMethod(typeof(Random))]
    public static void Prepare()
    {
        MRandom.Behavior = MoleBehaviors.Fallthrough;
        MRandom.AllInstances.NextInt32Int32 = (r, a, b) => { return 50; };
    }

I think to use another implementation

        var fake = new SRandom { CallBase = true };
        var mole = new MRandom(fake)
        {
            NextInt32Int32 = (a, b) => { return 1;  }
        };
        MRandom.BehaveAsCurrent();

but not work. For a little time I think that I cannot mock Random's method Next because in Mole Manual Reference is reported : .. the Moles framework to create mole types, which take advantage of runtime instrumentation. They are useful when dealing with APIs with static methods, sealed types, or nonvirtual methods ..

and Random's method Next is:

        public virtual int Next(
            int minValue,
            int maxValue
        )

Could someone give me an explanation because my "Prepare" method work ?

Thanks a lot, best regards.

Boymix81
  • 31
  • 4