4

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]
                   ^
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
  • I'm really having a hard time understanding the problem you're trying to solve here. I thought your initial question was about repeated parameters, now I'm wondering if it's about how to mock overloaded methods? Also scalamock is not able to mock anything other than Functions, traits, or interfaces. You need something like Mockito if you want to mock classes. – Travis Kaufman Jun 14 '15 at 21:35
  • Yes sorry about that i combined the thing because i though the error could be a cascade. I found the solution will write down the answer – MaatDeamon Jun 14 '15 at 21:52
  • Your statement is not accurate Travis: here is what scalamock is supposed to support: ScalaMock provides fully type-safe support for almost all Scala features. This includes: mocking classes, traits and case classes mocking functions and operators mocking type parametrised and overloaded methods support for type constraints support for repeated parameters and named parameters mocking Java classes and interfaces – MaatDeamon Jun 14 '15 at 21:57

2 Answers2

6

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.

MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
0
(item.addMetadata _).expects(Seq("", "", "", "", "", "", ""))

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

Travis Kaufman
  • 2,867
  • 22
  • 23
  • Thanks, i actually checked the overloaded part. Which match my case. But i still get issue. I will re-edit – MaatDeamon Jun 14 '15 at 21:35
  • Coming back on this issue I think I should precise the following. I was not trying to mock a method with repeated parameter that string*. But a function with a fixe number of parameter of the same type: 7 string parameters. However that function is overloaded. – MaatDeamon Jul 19 '15 at 17:10