Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions docs/datatypes/test/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,14 @@ import zio._
case class Counter(value: Ref[Int]) {
def inc: UIO[Unit] = value.update(_ + 1)
def get: UIO[Int] = value.get
def reset: UIO[Unit] = value.set(0)
}

object Counter {
val layer =
ZLayer.scoped(
ZIO.acquireRelease(
Ref.make(0).map(Counter(_)) <* ZIO.debug("Counter initialized!")
)(c => c.get.debug("Number of tests executed") *> c.reset)
)(c => c.get.debug("Number of tests executed"))
)
def inc = ZIO.service[Counter].flatMap(_.inc)
}
Expand Down Expand Up @@ -216,11 +215,9 @@ import zio.test._

object Spec1 extends SharedCounterSpec {
override def spec =
suite("spec1")(
test("test1") {
assertTrue(true)
} @@ TestAspect.after(Counter.inc)
)
test("test1") {
assertTrue(true)
} @@ TestAspect.after(Counter.inc)
}
```

Expand All @@ -232,31 +229,19 @@ import zio.test._

object Spec2 extends SharedCounterSpec {
override def spec: Spec[Scope with Counter, Any] =
suite("spec2")(
test("test1") {
assertTrue(true)
} @@ TestAspect.after(Counter.inc),
test("test2") {
assertTrue(true)
} @@ TestAspect.after(Counter.inc),
test("test2") {
assertTrue(true)
} @@ TestAspect.after(Counter.inc)
)
test("test2") {
assertTrue(true)
} @@ TestAspect.after(Counter.inc)
}
```

Now, when we run all specs (`sbt testOnly org.example.*`), we will see an output like this:

```
Counter initialized!
+ spec1
+ test1
+ spec2
+ test3
+ test2
+ test1
Number of tests executed: 4
+ test1
+ test2
Number of tests executed: 2
```

The ZIO test runner will execute all specs with the shared bootstrap layer. This means that the `Counter` service will be created and managed only once, and will be shared between all specs.
Expand Down