gpt4 book ai didi

具有多个参数的scalamock和方法

转载 作者:行者123 更新时间:2023-12-04 02:01:23 25 4
gpt4 key购买 nike

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

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

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

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

如何处理多个参数?

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


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

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

最佳答案

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

所以解决方案是写:

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

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

关于具有多个参数的scalamock和方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30834603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com