Skip to content

Commit

Permalink
fix readme, add resetStubsF
Browse files Browse the repository at this point in the history
  • Loading branch information
goshacodes committed Sep 30, 2024
1 parent c132013 commit 11074f2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 11 deletions.
85 changes: 76 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@

Then you've come to the right place, meet **backstub** - stubbing library for Scala 3 inspired by [scalamock](https://github.com/paulbutcher/ScalaMock) and compatible with any testing framework out of the box

How it works:
1. Setup cleanup after each test-case
2. Setup expected results based on input arguments per suit or per test-case
3. After stubs were used - get the data passed through the method and verify

## Setup

### Basic
Add to your build.sbt

```scala
Expand All @@ -27,20 +23,33 @@ Library hugely relies on experimental scala 3 features, so consider also adding
Test \ scalacOptions += "-experimental"
```

### ZIO

For ZIO integration also add:
```scala
libraryDependencies += "io.github.goshacodes" %% "backstub-zio" % "<version_from_badge>"
```

### Cats Effect

For Cats Effect integration also add:
```scala
libraryDependencies += "io.github.goshacodes" %% "backstub-cats-effect" % "<version_from_badge>"
```

## API

### Stubs
### Basic Stubs

Should be mixed with your test-suite, to provide clean-up API

```scala 3
package backstub

trait Stubs:
given stubs: CreatedStubs = CreatedStubs()

def resetStubs(): Unit = stubs.clearAll()
final given stubs: CreatedStubs = CreatedStubs()

final def resetStubs(): Unit = stubs.clearAll()
```

Using it as simple as:
Expand All @@ -54,6 +63,48 @@ class MySpec extends munit.FunSuite with Stubs:

```

### ZIO Stubs

Gives you instance of StubEffect, allowing to integrate ZIO

```scala 3
package backstub

import zio.*

trait ZIOStubs extends Stubs:
// this method is actually in Stubs
final def resetStubsIO[F[+_, +_]: StubEffect]: F[Nothing, Unit] =
summon[StubEffect[F]].unit(resetStubs())

given effect.StubEffect[IO] with
def unit[T](t: => T): UIO[T] = ZIO.succeed(t)
def flatMap[E, EE >: E, T, T2](fa: IO[E, T])(f: T => IO[EE, T2]): IO[EE, T2] = fa.flatMap(f)


```

### Cats Stubs

Gives you instance of StubEffect.Mono, allowing to integrate Cats Effect

```scala 3
package backstub

import cats.effect.*

trait CatsEffectStubs extends Stubs:
// this method is actually in Stubs
final def resetStubsF[F[+_]: StubEffect.Mono]: F[Unit] =
summon[StubEffect.Mono[F]].unit(resetStubs())

given StubEffect.Mono[IO] = new StubEffect.Mono[IO]:
def unit[T](t: => T): IO[T] = IO(t)
def flatMap[E, EE >: E, T, T2](fa: IO[T])(f: T => IO[T2]): IO[T2] = fa.flatMap(f)


```

### stub

Generates a stub. Without expectations setup just throws **NotImplementedError**
Expand Down Expand Up @@ -132,6 +183,14 @@ inline given Expect[Foo] = Expect[Foo]
val foo = stub[Foo]
```

### ZIO Expect

The only difference is - you should choose method using `methodIO`.

### Cats Effect Expect

The only difference is - you should choose method using `methodF0` (if no arguments) or `methodF`.


### Verify

Expand Down Expand Up @@ -161,6 +220,14 @@ foo.calls(_.twoArgs) // List((5, "foo"))

```

### Verify ZIO

You can use `callsIO` and `timesIO` methods

### Verify Cats Effect

You can use `callsF` and `timesF` methods

### Overloaded methods

To set expectations for overloaded methods - method type should be specified
Expand Down
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lazy val backstub = project
.in(file("."))
.settings(
commonSettings,
publishTo := None
publish / skip := true
)
.dependsOn(core, cats, zio)
.aggregate(core, cats, zio)
Expand All @@ -30,7 +30,7 @@ lazy val core = project.in(modules / "core")
)

lazy val zio = project.in(modules / "zio")
.dependsOn(core % "compile->compile;compile->test")
.dependsOn(core)
.settings(
name := "backstub-zio",
commonSettings,
Expand Down Expand Up @@ -74,3 +74,4 @@ inThisBuild(
)

sonatypeRepository := "https://s01.oss.sonatype.org/service/local"

3 changes: 3 additions & 0 deletions modules/core/src/main/scala/backstub/Stubs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ trait Stubs:

final def resetStubs(): Unit = stubs.clearAll()

final def resetStubsF[F[+_]: StubEffect.Mono]: F[Unit] =
summon[StubEffect.Mono[F]].unit(resetStubs())

final def resetStubsIO[F[+_, +_]: StubEffect]: F[Nothing, Unit] =
summon[StubEffect[F]].unit(resetStubs())

0 comments on commit 11074f2

Please sign in to comment.