簡體   English   中英

是否可以使用Mockito和Specs2模擬帶有視圖邊界的Scala方法?

[英]Is it possible to mock Scala methods with view bounds with Mockito and Specs2?

我在模擬使用Mockito和Specs2對其參數類型進行視圖邊界的方法時遇到了一個問題。 簡單地說,由於視圖綁定轉換為方法的額外隱式參數,因此Mockito無法將期望描述的調用與模擬接收的實際參數進行協調:

例如:

//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")
    }
  }
}

這個輸出給出:

[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

...有沒有人遇到過這個問題,或者使用specs2遇到任何使用視圖邊界或隱式參數模擬方法的方法?

謝謝,

蒂姆

蒂姆,我已經添加了驗證轉換參數到最新規格2 1.9-SNAPSHOT的可能性。

這仍然是實驗性的,因為我不會產生什么樣的不良影響。

例如,有一點是任何Function1參數現在默認都會被驗證為ok,因為Mockito無法知道哪些函數是隱式轉換,哪些是“常規”參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM