我正在尝试模拟一个具有多个参数的函数的对象。
我会尝试设定它的期望。也就是说,有些形式:
(item.addMetadata(,,,,,))。预计( “”, “”, “”, “” , “”, “”, “”)
我只是不知道如何写它。该示例通常处理一个参数函数:(item.addMetadata _)。expected(“”)
如何处理多重论证?
EDIT1
我改为 仅仅是为了编译 :
(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 { ^
作为一方而不是我还应该添加一个事实,即我在嘲笑一个类而不是一个接口。这是一个不受我控制的类,有一个私有构造函数,只有一个静态的create方法。所以我也得到以下错误:
Error:(18, 24) constructor Item in class Item cannot be accessed in <$anon: org.dspace.content.Item> val item = mock[Item] ^
答案 0 :(得分:4)
我需要的是处理对象的重载方法。我最初没想到这一点。
所以解决方案是写:
(item.addMetadata(_: String, _:String, _:String, _:String, _:String)) expects ("hi", "he", "hey", "test", "holla")
不确定如果它不是重载方法,那是必要的,这是我原始问题的一部分。
答案 1 :(得分:1)
(item.addMetadata _).expects(Seq("", "", "", "", "", "", ""))
请参阅:http://scalamock.org/user-guide/advanced_topics/#example-5---repeated-parameters