Closed MrPowers closed 4 years ago
Thank you for reporting! I don’t have any experience with mocking so I’m not familiar with what a library like mockito provides. My understanding is that mocking is helpful in certain kinds of codebases but mocking can often be avoided by using a different design (which might be more idiomatic Scala)
@olafurpg - Here's a code snippet where mocking would be useful:
def sendMessageToSlack(text: String): Unit = {
val slackApi = new SlackApi("someString")
val slackMessage = new SlackMessage()
slackMessage.setText(text)
slackApi.call(slackMessage)
}
def mySillyFunction(): Int = {
val res = 2 + 2
sendMessageToSlack("We just computed 2 + 2!!!")
res
}
Think you need mocking to be able to test sendMessageToSlack
and mySillyFunction
. In mySillyFunction
, we don't actually want to run the sendMessageToSlack
method. We just want to make sure that the method would be invoked with the argument "We just computed 2 + 2!!!"
.
I haven't used mockito-scala yet either. I will create a sample project and a blog post to discuss the key features. Will report back on this issue with my findings ;)
After more consideration I believe mocking is outside the scope of this library. I'm open to add integration modules with existing mocking libraries if there's demonstrable value (like with munit-scalacheck). I think any remotely interesting mocking library will quickly outgrow MUnit in complexity to merit a separate repo.
Sounds good, thanks for considering this.
My understanding is that mocking is helpful in certain kinds of codebases but mocking can often be avoided by using a different design (which might be more idiomatic Scala)
Hi @olafurpg! Would you mind elaborating on that? In my scenario I have a complex trait generated by smithy4s and I would like to mock only a handful of functions for a certain test. I'd be interested in knowing how to do that the idiomatic scala way.
Edit: In case anyone else reads this who is using smithy4s - they thought about that already and you can use this feature. So my problem is solved already, but I'm still curious on how to tackle this the scala-way.
It might be nice to add a native Scala mocking solution to MUnit. Your scalameta expertise might help ;)
I asked about the future of mocking in Scala and it seems like most folks use mockito-scala.
Looks like mockito-scala might only work with scalatest and specs2 - not sure.
ScalaMock can only mock traits and no-args classes . It looks like they were waiting for scala.meta to be released and then the project lost steam.
I found it easier to write mocks in other languages:
Let me know what you think!