Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capturing multiple arguments #348

Closed
twuyts opened this issue Oct 5, 2020 · 2 comments
Closed

Capturing multiple arguments #348

twuyts opened this issue Oct 5, 2020 · 2 comments

Comments

@twuyts
Copy link

twuyts commented Oct 5, 2020

Feature improvement request:

ScalaMock Version (e.g. 3.5.0)

5.0.0

Scala Version (e.g. 2.12)

2.12.

Please describe the expected behavior of the issue

Capturing multiple arguments is not intuitive: it requires a Capture object per argument, which makes verifying the captures cumbersome

Reproducible Test Case

"ScalaMock should capture the arguments of mocks - works, but cumbersome" in {
    trait TestTrait {
      def oneParam(i: Int): Unit
      def twoParam(i: Int, k: Int): Unit
      def threeParam(i: Int, j: Int, k: Int): Unit
    }

    val m = mock[TestTrait]
    val c1 = CaptureAll[Int]()
    val c21 = CaptureAll[Int]()
    val c22 = CaptureAll[Int]()

    m.oneParam _ expects capture(c1)
    m.twoParam _ expects (capture(c21), capture(c22)) repeat 2

    m.oneParam(99)
    m.twoParam(1,2)
    m.twoParam(3,4)
    c1.value should be (99)
    c21.value should be (3)
    c22.value should be (4)
    c21.values should contain theSameElementsInOrderAs Seq(1,3)
    c22.values should contain theSameElementsInOrderAs Seq(2,4)
}
"ScalaMock should capture the arguments of mocks - more intuitive" in {
    trait TestTrait {
      def oneParam(i: Int): Unit
      def twoParam(i: Int, k: Int): Unit
      def threeParam(i: Int, j: Int, k: Int): Unit
    }

    val m = mock[TestTrait]
    val c1 = CaptureAll[Int]()
    val c2 = CaptureAll[(Int, Int)]()
    val c3 = CaptureAll[(Int, Int, Int)]()

    m.oneParam _ expects capture(c1)
    m.twoParam _ expects capture(c2)
    m.threeParam _ expects capture(c3) repeat 2

    m.oneParam(99)
    m.twoParam(1,2)
    m.threeParam(1,2,3)
    m.threeParam(4,5,6)
    c1.value should be (99)
    c2.value should be ((1,2))
    c3.value should contain theSameElementsAs ((1,2,3),(4,5,6))
  }```
@barkhorn
Copy link
Collaborator

barkhorn commented Nov 3, 2020

I don't think what you describe above with the multiple arguments in a tuple is actually feasible in the Scala language, as your expectation is to convert from 1 arg of (Int, Int) to 2 args Int, Int dynamically.
It may well create a different problem when the tuple signature also exist and make it ambiguous in syntax.

Have you considered the onCall handler instead of argument captures?
You can write the lambda yourself then and perform more complex comparisons

https://scalamock.org/user-guide/advanced_topics/

@twuyts
Copy link
Author

twuyts commented Nov 3, 2020

You're probably right, I had a closer look at how the capturing works, and indeed, it does not seem possible.
I worked around it by writing my own mock.

@twuyts twuyts closed this as completed Nov 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants