scalamock and method with multiple argument

5.5k views Asked by At

i am trying to mock an object that has a function with multiple argument.

I would just try to set the expectation for it. That is, somehting of the form:

(item.addMetadata(,,,,,)).expects("","","","","","","")

I just don't know how to write it. The example, usually deal with one argument function: (item.addMetadata _).expects("")

How to deal with multiple argument ?

EDIT1

I change to Just for the sake of compiling:

(item.addMetadata _) expects (where {
      (schema: String, element: String, qualifier: String, lang: String, value: String) => true
    })

Now The problem apparently is that the method is overloaded ?

I get the following error:

Error:(21, 15) ambiguous reference to overloaded definition,
both method addMetadata in class Item of type (x$1: String, x$2: String, x$3: String, x$4: String, x$5: String, x$6: String, x$7: Int)Unit
and  method addMetadata in class Item of type (x$1: String, x$2: String, x$3: String, x$4: String, x$5: String)Unit
match expected type ?
        (item.addMetadata _) expects (where {
              ^

as a side not i should also added the fact that i am mocking a class and not an interface. This is class which is not under my control, has a private constructor and only a static create method. So i also get the following error:

Error:(18, 24) constructor Item in class Item cannot be accessed in <$anon: org.dspace.content.Item>
    val item = mock[Item]
                   ^
2

There are 2 answers

2
Travis Kaufman On
(item.addMetadata _).expects(Seq("", "", "", "", "", "", ""))

See: http://scalamock.org/user-guide/advanced_topics/#example-5---repeated-parameters

0
MaatDeamon On

What i needed was to deal with an overloaded method of an object. I did not figure that out initially.

So the solution is to write:

(item.addMetadata(_: String, _:String, _:String, _:String, _:String)) expects ("hi", "he", "hey", "test", "holla")

Not sure however what would have been necessary if it was not an overloaded method, which was part of my original question.