0

I am working on a test suite which has a helper method such as:

def setupMocks(isChild: Boolean = false): Unit

and in some of the tests, it is invoked as :

setupMocks(_)

whereas in other tests, it is invoked as :

setupMocks()

What does the _ do here? What is the significance? I did try the debugger but it simply skips over and I'm unable to figure this out.

Saturnian
  • 1,686
  • 6
  • 39
  • 65

1 Answers1

3

Because there is a default parameter, you can treat it both as a method with 1 and 0 parameters (kinda).

Underscores are basically placeholders for function parameters. setupMocks(_) is shorthand for x => setupMocks(isChild=x). See What are all the uses of an underscore in Scala?.

The second example is a plain method call, calling with isChild=false.

user
  • 7,435
  • 3
  • 14
  • 44
  • 1
    So what's really happening is that `setup(_)` returns a function which accepts one parameter and return `Unit`. Which is entirely different from `setupMocks()` which makes the actual call to the method! – Saturnian Dec 17 '20 at 05:47