2

解决了。IntelliJ 没有强调我的导入不完整这一事实。

你好,

我有一个尝试使用 jMock 开发的简单 Scala 程序。设置基本期望效果很好,但出于某种原因,Scala 不理解我从模拟对象返回值的尝试。我的 Maven 构建吐出以下错误

TestLocalCollector.scala:45: error: not found: value returnValue
one (nodeCtx).getParameter("FilenameRegex"); will( returnValue(regex))
                                                   ^

并且各自的代码片段是

@Before def setUp() : Unit = { nodeCtx = context.mock(classOf[NodeContext]) }
...
// the value to be returned
val regex = ".*\\.data"
...
// setting the expectations
one (nodeCtx).getParameter("FilenameRegex"); will( returnValue(regex))

对我来说,听起来 Scala 期望静态 jMock 方法returnValueval? 我在这里想念什么?

4

1 回答 1

2

你确定' ;'?

one (nodeCtx).getParameter("FilenameRegex") will( returnValue(regex))

可能会更好。

此示例中,您会看到如下一行:

  expect {
    one(blogger).todayPosts will returnValue(List(Post("...")))
  }

附有以下评论:

通过将“will”定义为 Scala 中缀运算符来指定返回值应该在同一表达式中。
在 Java 等效项中,我们必须进行单独的方法调用(我们最喜欢的 IDE 可能坚持将其放在下一行!)

  one(blogger).todayPosts; will(returnValue(List(Post("..."))))
                         ^
                         |
                         -- semicolon only in the *Java* version

OP自己解释:

returnValue静态方法不可见,因此出现错误。
而且该will方法只记录最新模拟操作的动作,这就是为什么它可以在下一行或分号之后:)

import org.jmock.Expectations 
import org.jmock.Expectations._ 
... 
context.checking( 
  new Expectations {
    { oneOf (nodeCtx).getParameter("FilenameRegex") will( returnValue(".*\\.data") ) }
  }
) 
于 2010-07-08T06:32:02.970 回答