Upgrading tests from old style/version #1240
-
Im trying to convert my tests that were on specs2 4.20 scala 2.13 and with specs-mock to latest with scala3 and scalamock.. im a little confused about the layout of tests.. previously we would build a trait context that extended scope and build ontop of this context many test cases but scope no longer seems available. previous test: trait Context extends Scope {
// mocks
// vals
def result: ???
}
"my test" >> {
"run with this context" in new Context {
result ==== 1
}
} first refactor: trait Context extends MockContext {
def result: ???
}
"my test" should {
"run with this context" in new Context {
result === 1 // this doesnt compile
}
} the error is exactly that it cannot convert the context to a result.
but if i do something like: trait Context extends MockContext {
def result = 1
}
"my test" should {
"run with this context" >> {
val c = new Context {}
c.result === 1
}
} this compiles however this feels incredibly cumbersome and falls over quickly on more complex test setups. surely im missing something ? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I might have thrown the baby out with the bath water...
That being said I understand how it can be syntactically useful so maybe it is time to re-introduce it. import org.specs2.execute.*
trait Scope
object Scope:
given scopeAsResult[S <: Scope]: AsResult[S] = new AsResult[S]:
def asResult(t: =>S): Result = AsResult.safely { Result.resultOrSuccess(t) }
// Example
class MySpec extends mutable.Specification:
"e1" in new MyScope:
someValue === 1
trait MyScope extends Scope:
val someValue: Int = 1 Then , please make a PR if you have a bit of time, or I will incorporate it in an upcoming release. |
Beta Was this translation helpful? Give feedback.
I might have thrown the baby out with the bath water...
Scope
was related with someBefore/After
traits that were usingDelayedInit
with disappear with Scala 3.Scope
involves some exception throwing inside the body of a trait definition, it has to be really well implemented otherwise the whole specification fails to be instantiated.That being said I understand how it can be syntactically useful so maybe it is time to re-introduce it.
You can use that code in the meantime: