可以用Mockito和Specs2来模拟Scala方法的视图边界吗?

可以用Mockito和Specs2来模拟Scala方法的视图边界吗?,scala,mocking,mockito,type-bounds,specs2,Scala,Mocking,Mockito,Type Bounds,Specs2,在使用Mockito和Specs2对参数类型具有视图边界的方法进行模拟时,我遇到了一个问题。简单地说,由于视图绑定转换为方法的额外隐式参数,Mockito无法将预期描述的调用与mock接收的实际参数协调: 例如: //Receiver.scala class Receiver { def methodWithViewBound[T <% WrappedString](w : T) : Unit = { //noop! } } //WrappedString.scala c

在使用Mockito和Specs2对参数类型具有视图边界的方法进行模拟时,我遇到了一个问题。简单地说,由于视图绑定转换为方法的额外隐式参数,Mockito无法将预期描述的调用与mock接收的实际参数协调:

例如:

//Receiver.scala
class Receiver {
  def methodWithViewBound[T <% WrappedString](w : T) : Unit = {
    //noop!
  }
}

//WrappedString.scala
class WrappedString(val wrapped : String) {

}

//TestMockedMethodWithViewBound.scala
import org.specs2.mutable._
import org.specs2.specification._
import org.specs2.mock._
import org.mockito.Matchers

class TestMockedMethodWithViewBound extends Specification with Mockito {

  implicit def wrapString(s : String) : WrappedString = new WrappedString(s);
  implicit def unwrapString(w : WrappedString) : String = w.wrapped;

  "Mockito" should {
    "Allow mocking of methods whose argument types include a view bound, using a matcher" in {
      val receiver = mock[Receiver]
      receiver.methodWithViewBound("Testing")
      there was one(receiver).methodWithViewBound(Matchers.eq("Testing")) 
    }


    "Allow mocking of methods whose argument types include a view bound, using an argument literal" in {
      val receiver = mock[Receiver]
      receiver.methodWithViewBound("Testing")
      there was one(receiver).methodWithViewBound("Testing")
    }
  }
}
//Receiver.scala
类接收器{
def methodWithViewBound[T在TestMockedMethodWithViewBound$$anonfun$1$$anonfun$apply$6$$anonfun$apply$2.apply$mcV$sp(TestMockedMethodWithViewBound.scala:22)
[错误]实际调用具有不同的参数:
[错误]receiver.methodWithViewBound(
[错误]“测试”,
[错误]($anonfun$apply$7)
[错误];
[错误]->在TestMockedMethodWithViewBound$$anonfun$1$$anonfun$apply$6.apply处(TestMockedMethodWithViewBound.scala:21)
[错误](TestMockedMethodWithViewBound.scala:19)
[信息]
[信息]
[信息]规格TestMockedMethodWithViewBound总计
[信息]在325毫秒内完成
[信息]2个示例(+1),2个预期(+1),2个故障(+1),0个错误
[错误]失败::总计2,失败2,错误0,通过0,跳过0
[错误]测试失败:
[错误]TestMockedMethodWithViewBound
[错误]{file:/home/tim/Projects/MockitoTest/}default-3adcf2/test:test:Tests未成功
[错误]总时间:7秒,已完成2012年3月7日15:23:05
…以前有没有人遇到过这个问题,或者遇到过使用specs2模拟具有视图边界或隐式参数的方法

谢谢


Tim

Tim,我已将验证转换参数的可能性添加到最新的specs2 1.9-SNAPSHOT中

这仍然是实验性的,因为我不知道会产生什么样的不期望的效果


例如,有一件事是,任何Function1参数现在都将在默认情况下被验证为ok,因为Mockito无法知道哪些函数是隐式转换,哪些是“常规”参数。

Eric,非常感谢!完全理解这是一个实验,事实上,考虑到它,我可以想出几种不同的方法(并且相互排斥)使用隐式参数模拟方法的行为可能是有效的,因此这不是一个真正明确的问题。然而,对于我的特定用例,这绝对是非常棒的,再次感谢!
[15:18:46] [tim@ahtanum ../tim/Projects/MockitoTest] $ sbt test
[info] Set current project to Mockito View Bounds Test (in build file:/home/tim/Projects/MockitoTest/)
[info] Compiling 1 Scala source to /home/tim/Projects/MockitoTest/target/scala-2.9.1/test-classes...
[info] Mockito should
[error] x Allow mocking of methods whose argument types include a view bound
[error]     The mock was not called as expected: 
[error] Invalid use of argument matchers!
[error] 2 matchers expected, 1 recorded.
[error] This exception may occur if matchers are combined with raw values:
[error]     //incorrect:
[error]     someMethod(anyObject(), "raw String");
[error] When using matchers, all arguments have to be provided by matchers.
[error] For example:
[error]     //correct:
[error]     someMethod(anyObject(), eq("String by matcher"));
[error] 
[error] For more info see javadoc for Matchers class. (TestMockedMethodWithViewBound.scala:12)
[info]  
[info]  
[info] Total for specification TestMockedMethodWithViewBound
[info] Finished in 175 ms
[info] 1 example, 1 failure, 0 error
[error] Failed: : Total 1, Failed 1, Errors 0, Passed 0, Skipped 0
[error] Failed tests:
[error]     TestMockedMethodWithViewBound
[error] {file:/home/tim/Projects/MockitoTest/}default-3adcf2/test:test: Tests unsuccessful
[error] Total time: 7 s, completed 07-Mar-2012 15:19:47
[15:19:47] [tim@ahtanum ../tim/Projects/MockitoTest] $ sbt test
[info] Set current project to Mockito View Bounds Test (in build file:/home/tim/Projects/MockitoTest/)
[info] Compiling 1 Scala source to /home/tim/Projects/MockitoTest/target/scala-2.9.1/test-classes...
[info] Mockito should
[error] x Allow mocking of methods whose argument types include a view bound, using a matcher
[error]     The mock was not called as expected: 
[error] Invalid use of argument matchers!
[error] 2 matchers expected, 1 recorded.
[error] This exception may occur if matchers are combined with raw values:
[error]     //incorrect:
[error]     someMethod(anyObject(), "raw String");
[error] When using matchers, all arguments have to be provided by matchers.
[error] For example:
[error]     //correct:
[error]     someMethod(anyObject(), eq("String by matcher"));
[error] 
[error] For more info see javadoc for Matchers class. (TestMockedMethodWithViewBound.scala:12)
[error] x Allow mocking of methods whose argument types include a view bound, using an argument literal
[error]     The mock was not called as expected: 
[error] Argument(s) are different! Wanted:
[error] receiver.methodWithViewBound(
[error]     "Testing",
[error]     ($anonfun$apply$mcV$sp$2) <function1>
[error] );
[error] -> at TestMockedMethodWithViewBound$$anonfun$1$$anonfun$apply$6$$anonfun$apply$2.apply$mcV$sp(TestMockedMethodWithViewBound.scala:22)
[error] Actual invocation has different arguments:
[error] receiver.methodWithViewBound(
[error]     "Testing",
[error]     ($anonfun$apply$7) <function1>
[error] );
[error] -> at TestMockedMethodWithViewBound$$anonfun$1$$anonfun$apply$6.apply(TestMockedMethodWithViewBound.scala:21)
[error]  (TestMockedMethodWithViewBound.scala:19)
[info]  
[info]  
[info] Total for specification TestMockedMethodWithViewBound
[info] Finished in 325 ms
[info] 2 examples (+1), 2 expectations (+1), 2 failures (+1), 0 error
[error] Failed: : Total 2, Failed 2, Errors 0, Passed 0, Skipped 0
[error] Failed tests:
[error]     TestMockedMethodWithViewBound
[error] {file:/home/tim/Projects/MockitoTest/}default-3adcf2/test:test: Tests unsuccessful
[error] Total time: 7 s, completed 07-Mar-2012 15:23:05