【问题标题】:Spec Explorer - Abstract specification of behaviourSpec Explorer - 行为的抽象规范
【发布时间】:2013-02-15 22:17:47
【问题描述】:

我目前正在评估 Spec Explorer,但我遇到了一个关于函数行为抽象规范的问题。 我有类似的东西:

[TypeBinding("Implementation.ImplementationElement")]
public class ModelElement
{ /*... */ }
public class ModelBehaviour
{
  [Rule]
  public static void doSomething()
  {
    ModelElement sel = SelectElement(elements);
    // ... do something with sel
  }
  private static Set<ModelElement> elements = new Set<ModelElement>();
}

现在我不想在模型程序中明确定义SelectElement(Set&lt;ModelElement&gt; e)。我更愿意用elements.contains(\result); 之类的后置条件来指定它。这有可能吗?

明确定义的问题是我会强制执行选择策略。

我试图通过以下方式避免该问题(也许我只是遗漏了一些小东西,有人可以给我提示以正确地做到这一点):

  1. 将参数ModelElement e添加到doSomething
  2. 将条件Condition.IsTrue(elements.Contains(e)) 添加到doSomething
  3. 在配置脚本SelectElement中定义一个动作
  4. 在config-Script中定义一台机器SelectAndDo如下:

    machine SelectAndDo() : Main
    {
      let ImplementationElement e 
          Where {.Condition.IsTrue(e.Equals(SelectElement()));.} 
          in doSomething(e)
    }
    
  5. 使用SelectAndDo 代替doSomething

但是,这不起作用,因为对相应模型的探索进入了错误状态。 如果这根本不起作用,那么 Windows 上的 Spec Explorer 是否有一个很好的替代品,最好是稳定的?是否可以推荐 FsCheck 用于测试有状态系统?

【问题讨论】:

    标签: c# testing specifications mbt


    【解决方案1】:

    我发现了问题所在。 上面概述的解决方案实际上有效,但如果元素为空,我从SelectElement() 返回null,因此无法满足 where 子句中的条件。因此,我决定返回一个类似于 Null 对象的“非法”元素,而不是返回 null。 所以我的整个解决方案看起来像这样:

    机器:

    machine Full() : Main
    {
      Init(); CreateElement();CreateOtherElement();CreateIllegal(); SelectAndDo* || ModelProgram
    }
    

    CreateIllegal() 是必需的,这样SelectAndDo 中的条件就可以满足。

    除此之外,我在模型程序中添加了对这个非法值的检查。

    编辑:

    实际上有一种更好、更直接的方式使用Choice.Some&lt;T&gt;,我不知道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 2011-11-22
      • 1970-01-01
      相关资源
      最近更新 更多