【问题标题】:Mole System.Random classMole System.Random 类
【发布时间】:2012-09-15 17:25:03
【问题描述】:

我有以下方法可以测试:

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 建议我使用 Random.Next 摩尔及其创建:

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)
}

参数化单元测试

[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);
}

以及模拟随机类的prepare方法:

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

我开发了模拟 Random 类的 prepare 方法

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;

但我得到以下 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);
}

异常详情

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) 

有人可以帮忙吗?

【问题讨论】:

  • 真正的问题是什么?这是作业吗?
  • 嗨 Pickypg ,我想做一篇关于 Pex 和 Moles 的论文,所以我正在研究 pex 的行为。问题是:“当单元测试调用方法 Random.Next 时,我如何才能使 Rando 类始终传递“1”?”非常感谢,最好的问候。
  • 你应该看看微软的 Fakes 而不是 Moles。 Moles 已被弃用,Fakes 是它的替代品(顺便说一下,由 MS Research 的同一团队提供)。 msdn.microsoft.com/en-us/library/hh549175.aspx

标签: c# random moles pex


【解决方案1】:

我在准备方法中找到了我的问题的解决方案:

准备()

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

我想使用另一种实现方式

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

但不起作用。有一段时间我认为我无法模拟 Random 的 Next 方法,因为在 Mole Manual Reference 中报告了: .. Moles 框架来创建 moles 类型,它利用运行时检测。它们在处理具有静态方法、密封类型或非虚拟方法 ..

的 API 时很有用

和Random的方法Next是:

        public virtual int Next(
            int minValue,
            int maxValue
        )

谁能给我一个解释,因为我的“准备”方法有效?

非常感谢,最好的问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多