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

Add ability to capture arguments #234

Closed
vakhtang opened this issue Jul 13, 2018 · 6 comments
Closed

Add ability to capture arguments #234

vakhtang opened this issue Jul 13, 2018 · 6 comments
Milestone

Comments

@vakhtang
Copy link

vakhtang commented Jul 13, 2018

As far as I can tell ScalaMock lacks the ability to capture arguments a la Mockito's ArgumentCaptor. This StackOverflow post alludes to a mechanism that exists under-the-hood. It'd be great to expose it, or if it doesn't actually exist, add it. I imagine this can be implemented as a custom matcher.

@vakhtang vakhtang changed the title Add ability to capture an argument Add ability to capture arguments Jul 13, 2018
@barkhorn
Copy link
Collaborator

See http://scalamock.org/user-guide/advanced_topics/ , topic "Returning values (onCall)"
In your onCall function, just save the value you're interested in into some field you defined above - done.

It's neither internal or undocumented :)

@vakhtang
Copy link
Author

I'm aware of that, and was looking for something a bit more first-class, but ok!

@barkhorn
Copy link
Collaborator

i don't know what "more first-class" means. looking for some syntactic sugar?

@vakhtang
Copy link
Author

vakhtang commented Jul 16, 2018

Yes, more sugar. Perhaps something like this:

trait Foo {
  def increment(a: Int): Int
}

val fooMock = mock[Foo]
val captor = capture[Int]

(foo.increment _).expects(captor).once()

println(captor.value)

If there's an appetite for this I can take a stab at it.

@barkhorn barkhorn reopened this Jul 24, 2018
@barkhorn barkhorn added this to the v4.2.0 milestone Jul 24, 2018
@barkhorn
Copy link
Collaborator

got something on a branch that works like this:

  it should "capture the arguments of mocks - capture one" in {
    val m = mock[TestTrait]
    val c1 = CaptureOne[Int]()

    m.oneParam _ expects capture(c1) once()
    m.oneParam(42)
    c1.value should be (42)
  }

  it should "capture the arguments of mocks - capture all" in {
    val m = mock[TestTrait]
    val c = CaptureAll[Int]()

    m.oneParam _ expects capture(c) repeat 3
    m.oneParam(99)
    m.oneParam(17)
    m.oneParam(583)
    c.value should be (583)
    c.values should be (Seq(99, 17, 583))
  }

I'll push it later and you can review / comment @vakhtang ?

@vakhtang
Copy link
Author

Looks great, thanks!

barkhorn added a commit to barkhorn/ScalaMock that referenced this issue Jul 24, 2018
barkhorn added a commit to barkhorn/ScalaMock that referenced this issue Jul 24, 2018
@barkhorn barkhorn mentioned this issue Jul 24, 2018
3 tasks
barkhorn added a commit that referenced this issue Jul 24, 2018
added argument capturing. fixes #234
barkhorn added a commit that referenced this issue Jul 24, 2018
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