2015-11-03 65 views
0

我使用specs2作爲我的測試框架。 我想生成一個在測試中可用的uniq密鑰。specs2 - 如何在測試本身內部和內部使用相同的變量?

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

找不到任何好辦法做到這一點.. 任何想法?

回答

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

0

你應該從這個僞得到的總體思路:

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