Referring to a parameterless function

In short: how can I refer to a parameterless function, not the results of a parameterless function?

I have a class like this:

class Temp {
def content: String
def someFunc(): String
}

And I am using ScalaMock to mock it with code like:

val myMock = mock[Temp]
(myMock.someFunc _).expects().returning(“Some String”).once()

In this code, the object myMock was created with a property “someFunc” that is a function; in this case of type MockFunction0[String], and the MockFunction0 object has a method “expects”. ScalaMock is correctly identifying “content” as a MockFunction0 (maybe that’s not correct; I don’t know); I can step through and see the creation of the object. What I can’t do is refer to the object itself. If I try:

(myMock.content _).expects()

it won’t compile. I get " _ must follow method; cannot follow fixture.mockResponse.body.type". If I try:

(myMock.content).expects()

it thinks the method “expects” is on a String object. I can’t figure out how to access the MockFunction0 object. Is there a syntax I don’t know about that will let me do this?

I never used ScalaMock but if you need to reference your function as () => String you will have to be a little more explicit by typing () => content

1 Like

Scala follows the uniform access principle, so a val is identical to the parameterless def, there is no eta-expansion (foo.bar _) for them.

2 Likes