Packages

  • package root
    Definition Classes
    root
  • package zio
    Definition Classes
    root
  • package test

    _ZIO Test_ is a featherweight testing library for effectful programs.

    _ZIO Test_ is a featherweight testing library for effectful programs.

    The library imagines every spec as an ordinary immutable value, providing tremendous potential for composition. Thanks to tight integration with ZIO, specs can use resources (including those requiring disposal), have well- defined linear and parallel semantics, and can benefit from a host of ZIO combinators.

    import zio.test._
    import zio.clock.nanoTime
    import Assertion.isGreaterThan
    
    object MyTest extends DefaultRunnableSpec {
      suite("clock") {
        testM("time is non-zero") {
          assertM(nanoTime, isGreaterThan(0))
        }
      }
    }
    Definition Classes
    zio
  • package mock
    Definition Classes
    test
  • Live
  • MockClock
  • MockConsole
  • MockEnvironment
  • MockRandom
  • MockSystem
  • package reflect
    Definition Classes
    test
p

zio.test

mock

package mock

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. mock
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. trait Live[+R] extends AnyRef
  2. trait MockClock extends Clock with Scheduler

    MockClock makes it easy to deterministically and efficiently test effects involving the passage of time.

    MockClock makes it easy to deterministically and efficiently test effects involving the passage of time.

    Instead of waiting for actual time to pass, sleep and methods implemented in terms of it schedule effects to take place at a given clock time. Users can adjust the clock time using the adjust and setTime methods, and all effects scheduled to take place on or before that time will automically be run.

    For example, here is how we can test ZIO.timeout using MockClock:

    import zio.ZIO
    import zio.duration._
    import zio.test.mock.MockClock
    
    for {
      fiber  <- ZIO.sleep(5.minutes).timeout(1.minute).fork
      _      <- MockClock.adjust(1.minute)
      result <- fiber.join
    } yield result == None

    Note how we forked the fiber that sleep was invoked on. Calls to sleep and methods derived from it will semantically block until the time is set to on or after the time they are scheduled to run. If we didn't fork the fiber on which we called sleep we would never get to set the the time on the line below. Thus, a useful pattern when using MockClock is to fork the effect being tested, then adjust the clock to the desired time, and finally verify that the expected effects have been performed.

    Sleep and related combinators schedule events to occur at a specified duration in the future relative to the current fiber time (e.g. 10 seconds from the current fiber time). The fiber time is backed by a FiberRef and is incremented for the duration each fiber is sleeping. Child fibers inherit the fiber time of their parent so methods that rely on repeated sleep calls work as you would expect.

    For example, here is how we can test an effect that recurs with a fixed delay:

    import zio.Queue
    import zio.duration._
    import zio.test.mock.MockClock
    
    for {
      q <- Queue.unbounded[Unit]
      _ <- (q.offer(()).delay(60.minutes)).forever.fork
      a <- q.poll.map(_.isEmpty)
      _ <- MockClock.adjust(60.minutes)
      b <- q.take.as(true)
      c <- q.poll.map(_.isEmpty)
      _ <- MockClock.adjust(60.minutes)
      d <- q.take.as(true)
      e <- q.poll.map(_.isEmpty)
    } yield a && b && c && d && e

    Here we verify that no effect is performed before the recurrence period, that an effect is performed after the recurrence period, and that the effect is performed exactly once. The key thing to note here is that after each recurrence the next recurrence is scheduled to occur at the appropriate time in the future, so when we adjust the clock by 60 minutes exactly one value is placed in the queue, and when we adjust the clock by another 60 minutes exactly one more value is placed in the queue.

  3. trait MockConsole extends Console
  4. case class MockEnvironment(blocking: Service[Any], clock: Mock, console: Mock, live: Service[Clock with Console with System with Random with Blocking], random: Mock, scheduler: Mock, sized: Service[Any], system: Mock) extends Blocking with Live[Clock with Console with System with Random with Blocking] with MockClock with MockConsole with MockRandom with MockSystem with Scheduler with Sized with Product with Serializable
  5. trait MockRandom extends Random
  6. trait MockSystem extends System

Value Members

  1. def live[R, E, A](zio: ZIO[R, E, A]): ZIO[Live[R], E, A]

    Provides an effect with the "real" environment as opposed to the mock environment.

    Provides an effect with the "real" environment as opposed to the mock environment. This is useful for performing effects such as timing out tests, accessing the real time, or printing to the real console.

  2. val mockEnvironmentManaged: Managed[Nothing, MockEnvironment]
  3. def withLive[R, R1, E, E1, A, B](zio: ZIO[R, E, A])(f: (IO[E, A]) ⇒ ZIO[R1, E1, B]): ZIO[R with Live[R1], E1, B]

    Transforms this effect with the specified function.

    Transforms this effect with the specified function. The mock environment will be provided to this effect, but the live environment will be provided to the transformation function. This can be useful for applying transformations to an effect that require access to the "real" environment while ensuring that the effect itself uses the mock environment.

    withLive(test)(_.timeout(duration))
  4. object Live
  5. object MockClock extends Serializable
  6. object MockConsole extends Serializable
  7. object MockEnvironment extends Serializable
  8. object MockRandom extends Serializable
  9. object MockSystem extends Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped