fmonniot / scala3mock

Mocking framework for Scala 3
https://francois.monniot.eu/scala3mock
Other
19 stars 1 forks source link

Integration with Test frameworks that require to return some special type from tests #26

Closed longliveenduro closed 7 months ago

longliveenduro commented 7 months ago

How would I integrate "withExpectations" in a test framework that expects a test to return a special type?

E.g. weaver for Cats Effect

longliveenduro commented 7 months ago

Ah sorry, found it...easy..

test("validate password reset link"):
    withExpectations():
      val authRepo  = mock[AuthenticationRepository]
      val realmUtil = mock[RealmUtil]
      val config    = AuthenticationServiceConfig("frontendUrl", verificationNeeded = false)

      val authServiceIO = AuthenticationService.configure(authRepo, realmUtil, realmUtil, realmUtil, config)

      for
        _   <- authServiceIO
        two <- 2.pure[IO]
      yield expect(two == 2)
fmonniot commented 7 months ago

Glad to know you found a working solution.

Be aware that the expectation are being verified (by default) when the function passed to withExpectations returns, which might not do exactly what you want with a lazy data structure like IO.

If you do not care about verifying expectations, you can disable them with withExpectations(verifyAfterRun = false). If you do care about them, I'm not entirely sure it's possible without slightly changing withExpectations itself.

longliveenduro commented 7 months ago

Thank you very much for your information @fmonniot !

Do you have any proposal how I can verify expectations with IO or do I have to wait for a solution on scala3mock side?

fmonniot commented 6 months ago

I'm working on a simple cats module. I'm hoping to have it out as a snapshot over the weekend. Note that the next version will have some breaking changes to reduce the number of imports when using the lib.

You can also redefine the withExpectations function in your own code if you don't want to wait.

fmonniot commented 6 months ago

I have released a new version with support for cats, that should help with weaver support.

longliveenduro commented 6 months ago

Thank you so much @fmonniot, I will check it out right now.

Have a wonderful day!

longliveenduro commented 6 months ago

@fmonniot 0.6.0 works like a charm with IO. Thank you so much.

Do you know if it works with ZIO as well?

fmonniot commented 6 months ago

As long as there is a MonadError instance in scope it should yes. No idea what the state of interop between cats and zio is these days.