StubToolsFx.stub is a utility for creating simple function-based stubs for testing, removing the need for heavy mock frameworks. It accepts an Option containing the stub implementation. If the Option is None, the tool raises a MissingStubException which includes the exact line number where the stub was missing, providing better traceability than a NullPointerException.
Depending on the required return type and the presence of cats typeclasses (Functor, Monad), StubToolsFx.stub can be used to handle simple values, functions, or effectful computations.
import cats.{Functor, Monad}
import cats.syntax.all._
import effectie.core.Fx
import extras.testing.StubToolsFx
// 1. Simple value stub
def fooStub[F[_]: Fx](f: => Option[A]): FooStub[F] = new FooStub[F] {
def foo: F[A] = StubToolsFx.stub(f)
}
// 2. Stub returning a function (requires Functor)
def fooStub[F[_]: Fx: Functor](f: => Option[() => A]): FooStub[F] = new FooStub[F] {
def foo(): F[A] = StubToolsFx.stub(f).map(_())
}
// 3. Stub returning a function with arguments (requires Functor)
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))
}
// 4. Stub returning an effectful function (requires Monad)
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))
}