Skip to main content

Extras - Testing Tools for Effectie

StubToolsFx.stub

StubToolsFx.stub is a tool for a stub (a simple function for testing) so that you don't need to use mock frameworks.

import cats.{Functor, Monad}
import cats.syntax.all._

import effectie.core.Fx

import extras.testing.StubToolsFx
def fooStub[F[_]: Fx](f: => Option[A]): FooStub[F] = new FooStub[F] {
def foo: F[A] = StubToolsFx.stub(f) // F[A]
}
// If f is None, it will raise MissingStubException with the line number pointing where it's missing
def fooStub[F[_]: Fx: Functor](f: => Option[() => A]): FooStub[F] = new FooStub[F] {
def foo(): F[A] = StubToolsFx.stub(f).map(_()) // F[A]
}
// If f is None, it will raise MissingStubException with the line number pointing where it's missing
def fooStub[F[_]: Fx: Functor](f: => Option[A => B]): FooStub[F] = new FooStub[F] {
def foo(a: A): F[B] = StubToolsFx.stub(f).map(_(a)) // F[B]
}
// If f is None, it will raise MissingStubException with the line number pointing where it's missing
def fooStub[F[_]: Fx: Monad](f: => Option[A => F[B]]): FooStub[F] = new FooStub[F] {
def foo(a: A): F[B] = StubToolsFx.stub(f).flatMap(_(a)) // F[B]
}
// If f is None, it will raise MissingStubException with the line number pointing where it's missing

Example

e.g.)

import cats.Monad
import cats.syntax.all._
import eu.timepit.refined.types.all._
import eu.timepit.refined.cats._
import eu.timepit.refined.auto._
import io.estatico.newtype.macros.newtype
import extras.testing.StubToolsFx

import effectie.core._
import effectie.syntax.all._

object types {
@newtype case class Id(value: PosInt)
@newtype case class Name(value: NonEmptyString)
}
import types._

trait MyService[F[_]] {
def getName(id: Id): F[Option[Name]]
}

object MyServiceStub {
def apply[F[_]: Fx: Monad](f: => Option[Id => F[Option[Name]]]): MyService[F] = new MyService[F] {
override def getName(id: Id): F[Option[Name]] = StubToolsFx.stub(f).flatMap(_(id))
}
}

class Hello[F[_]: Monad](myService: MyService[F]) {
def hello(id: Id): F[String] = {
myService.getName(id)
.map { maybeName =>
maybeName.fold(s"No name found for id ${id.value}")(name => s"Hello ${name.value}")
}
}
}

val expectedId = Id(1)
// expectedId: Id = 1
val expectedName = Name("Kevin")
// expectedName: Name = Kevin

import cats.effect._

import effectie.instances.ce2.fx._

val myService: MyService[IO] = MyServiceStub(((id: Id) =>
if (id.value === expectedId.value)
pureOfSome(expectedName)
else
pureOfNone
).some)
// myService: MyService[IO] = repl.MdocSession$MdocApp0$MyServiceStub$$anon$1@63b9924f
val hello = new Hello[IO](myService)
// hello: Hello[IO] = repl.MdocSession$MdocApp0$Hello@22f322f3
hello.hello(Id(1))
.map(println)
.unsafeRunSync()
// Hello Kevin
hello.hello(Id(2))
.map(println)
.unsafeRunSync()
// No name found for id 2
/* If you don't expect Hello to use MyService.getName,
* you can simply remove feeding the function for that operation
* and StubTools let you know where it fails if Hello uses MySerivce.getName.
*/
val myService2: MyService[IO] = MyServiceStub(none)
// myService2: MyService[IO] = repl.MdocSession$MdocApp0$MyServiceStub$$anon$1@610632d8
val hello2 = new Hello(myService2)
// hello2: Hello[[A]IO[A]] = repl.MdocSession$MdocApp0$Hello@70bfbeb0
hello2.hello(Id(1))
.attempt
.map(println)
.unsafeRunSync()
// Left(extras.testing.StubTools$MissingStubException:
// >> Missing Stub implementation at
// >> repl.MdocSession$MdocApp0$MyServiceStub$$anon$1$$anonfun$getName$1.apply(effectie.md:54)
// >> ---
// >> Details:
// >> repl.MdocSession$MdocApp0$MyServiceStub$$anon$1$$anonfun$getName$1.apply(effectie.md:54)
// at repl.MdocSession$MdocApp0$MyServiceStub$$anon$1$$anonfun$getName$1.apply(effectie.md:54)
// at cats.effect.IO$.fromOption(IO.scala:1460)
// at effectie.instances.ce2.fxCtor$ioFxCtor$.fromOption(fxCtor.scala:26)
// at effectie.instances.ce2.fxCtor$ioFxCtor$.fromOption(fxCtor.scala:11)
// at effectie.instances.ce2.fx$ioFx$.fromOption(fx.scala:27)
// at effectie.instances.ce2.fx$ioFx$.fromOption(fx.scala:10)
// at extras.testing.StubToolsFx$StubToolsFxPartiallyApplied$.$anonfun$apply$1(StubToolsFx.scala:26)
// at cats.effect.internals.IORunLoop$.step(IORunLoop.scala:319)
// at cats.effect.IO.unsafeRunTimed(IO.scala:338)
// at cats.effect.IO.unsafeRunSync(IO.scala:256)
// at repl.MdocSession$MdocApp0$.<clinit>(effectie.md:122)
// at repl.MdocSession$MdocApp.<init>(effectie.md:5)
// at repl.MdocSession$.app(effectie.md:3)
// at mdoc.internal.document.DocumentBuilder$$doc$.$anonfun$build$2(DocumentBuilder.scala:89)
// at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
// at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
// at scala.Console$.withErr(Console.scala:193)
// at mdoc.internal.document.DocumentBuilder$$doc$.$anonfun$build$1(DocumentBuilder.scala:89)
// at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
// at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
// at scala.Console$.withOut(Console.scala:164)
// at mdoc.internal.document.DocumentBuilder$$doc$.build(DocumentBuilder.scala:88)
// at mdoc.internal.markdown.MarkdownBuilder$.$anonfun$buildDocument$2(MarkdownBuilder.scala:47)
// at mdoc.internal.markdown.MarkdownBuilder$$anon$1.run(MarkdownBuilder.scala:104)
// )
NOTE

Why not just use mock framework for convenience? To answer that, please read Pitfalls of Mocking in tests from Xebia Functional (formerly known as 47 Degrees)

Besides what the blog tells you, mock frameworks often make you do bad practice like testing the implementation details with verify.

There is also an issue when your mock is not correctly set. You may get a NullPointerException for that, but it doesn't tell you where it's from and why you get it.