【问题标题】:Forcing order of excecution with Specs2 Acceptance Style Testing使用 Specs2 验收样式测试强制执行顺序
【发布时间】:2012-11-19 08:43:02
【问题描述】:

我正在尝试使用 Specs2 验收样式测试让我的测试按顺序执行,但我没有任何运气。

  override def is = {     
    "Template Project REST Specification" ^
      p ^
      "The server should" ^
      "Respond with greeting on root path" ! serverRunning ^
      p ^
      "For CLIENT json objects" ^
      "Return an empty list if there are no entities" ! getEmptyClientList ^
      "Create a new entity" ! createClient ^
      "Return a non-empty list if there some entities" ! getNonEmptyClientList ^
      "Read existing" ! todo ^
      "Update existing" ! todo ^
      "Delete existing" ! todo ^
      "Handle missing fields" ! todo ^
      "Handle invalid fields" ! todo ^
      "Return error if the entity does not exist" ! todo ^
      end
  }

运行测试时,createClient 测试会在getEmptyClientList 测试有机会执行之前不断创建新的客户端元素。

如果我在createClient 测试之前添加一整堆getEmptyClientList 测试,那么除了最后一个之外的所有测试都将在调用createClient 之前执行。但是createClient 总是会击败最后一个getEmptyClientList 调用,这会导致它失败。

如何强制它按顺序执行?使用 Specs2 单元测试风格,我只是在测试前添加了 sequential 关键字,一切都会好起来的。

【问题讨论】:

    标签: scala specs2


    【解决方案1】:

    在验收规范中,sequential 参数可以像这样添加到规范的开头:

    def is = sequential ^
      "Template Project REST Specification" ^
       p ^
       "The server should" ^
       "Respond with greeting on root path" ! serverRunning ^
       p ^
       "For CLIENT json objects" ^
       "Return an empty list if there are no entities" ! getEmptyClientList ^
       "Create a new entity" ! createClient ^
       "Return a non-empty list if there some entities" ! getNonEmptyClientList ^
       "Read existing" ! todo ^
       "Update existing" ! todo ^
       "Delete existing" ! todo ^
       "Handle missing fields" ! todo ^
       "Handle invalid fields" ! todo ^
       "Return error if the entity does not exist" ! todo ^
       end
    

    请注意,您不需要override is 方法,也不需要花括号。

    【讨论】:

    • 谢谢埃里克,这很有效。关于“覆盖”的评论:如果我省略“覆盖”,编译器会抱怨“org.specs2.specification.Fragments”中的方法“是”需要一个覆盖修饰符。
    • 你确定你使用的是org.specs2.Specification(它有一个抽象的is方法)而不是org.specs2.mutable.Specification(它有一个具体的is方法)吗?
    • 哈,这就是问题所在 - 已修复 ;-) 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多