3

我正在尝试模拟一个具有多个参数的函数的对象。

我只是试着为它设定期望。也就是说,某种形式:

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

我只是不知道怎么写。该示例,通常处理一个参数函数: (item.addMetadata _).expects("")

如何处理多个参数?

编辑1

我更改为只是为了编译

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

现在问题显然是方法重载了?

我收到以下错误:

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 {
              ^

作为一个方面,我还应该补充一个事实,即我在模拟一个类而不是一个接口。这是不受我控制的类,有一个私有构造函数,只有一个静态创建方法。所以我也收到以下错误:

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

2 回答 2

6

我需要的是处理对象的重载方法。我一开始并没有想到这一点。

所以解决方案是写:

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

但是,不确定如果它不是一个重载方法,那将是必要的,这是我最初问题的一部分。

于 2015-06-14T21:55:32.313 回答
0
(item.addMetadata _).expects(Seq("", "", "", "", "", "", ""))

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

于 2015-06-14T21:24:01.650 回答