【问题标题】:specs2 - how to use the same variable inside around and inside the test itself?specs2 - 如何在测试本身内部和内部使用相同的变量?
【发布时间】:2015-11-03 08:28:14
【问题描述】:

我使用 specs2 作为我的测试框架。 我想生成一个在测试本身中可用的 uniq 密钥。

def around[R: AsResult](r: => R): Result = {
   val uniqueToken = before()
   try AsResult(r)(uniqueToken)
   finally after(uniqueToken)
}

"foo" should {
   "bar" in {
     do something with uniqueToken
   }
}

找不到任何好的方法来做到这一点.. 有什么想法吗?

【问题讨论】:

    标签: scala unit-testing mockito specs2


    【解决方案1】:

    你可以这样写

    class MySpec extends Specification with ForEach[Token] {
      "foo" should {
         "do something" in { token: Token =>
            ok
         }
      }
    
      def foreach[R : AsResult](f: Token => R): Result = {
        val token = createToken
    
        try AsResult(f(token))
        finally cleanup(token)
      }
    }
    

    这已记录在here

    【讨论】:

      【解决方案2】:

      你应该从这个伪代码中得到大致的想法:

      class Around[R: AsResult](r: => R) {
         val uniqueToken = before()
      
         try AsResult(r)(uniqueToken)
         finally after(uniqueToken)
      }
      
      "foo" should {
         "bar" in new Around(r) {
           do something with uniqueToken
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-20
        • 1970-01-01
        • 2012-09-25
        • 2013-02-21
        相关资源
        最近更新 更多