【问题标题】:Pex not satisfying code contractPex 不满足代码合同
【发布时间】:2011-07-28 17:20:26
【问题描述】:

我正在尝试编写一个 pex 测试,我注意到它总是提供一个错误值作为我想要的参数之一。我的测试看起来像这样(简化:有/还有更多参数,但没有什么不同):

[PexMethod]
public void TestCtor(bool value)
{
    ArbitraryType myType = new ArbitraryType(value);
}

我想测试一个让 pex 进行探索的场景,确保 value 为真。我做了另一个看起来像这样的测试:

[PexMethod]
public void TestCtor(bool value)
{
    Contract.Requires(value == true);

    ArbitraryType myType = new ArbitraryType(value);
}

但是当我让 Pex 探索它时,它仍然向 value 吐出错误信息,并且它生成的测试“通过”。如果我在Contract.Assert(!value); 的要求后面加上一行,它将创建另一个测试并为value 传递 true 以使断言失败。

问题是,为什么 Pex 不满足代码合同?

【问题讨论】:

  • 我假设您在运行时启用了“工具”->“选项”->“代码合同”下的合同检查?

标签: code-contracts pex pex-and-moles


【解决方案1】:

我不确定 Pex 将在测试方法中对合同做什么,但我看不出这是一件好事 :)

如果您希望 Pex 执行此操作,正确的做法是使用 PexAssume.IsTrue(value)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多