[英]How to do an instanceof check with Scala(Test)
我正在嘗試將 ScalaTest 合並到我的 Java 項目中; 用 ScalaTests 替換所有 JUnit 測試。 有一次,我想檢查 Guice 的 Injector 是否注入了正確的類型。 在 Java 中,我有一個這樣的測試:
public class InjectorBehaviour {
@Test
public void shouldInjectCorrectTypes() {
Injector injector = Guice.createInjector(new ModuleImpl());
House house = injector.getInstance(House.class);
assertTrue(house.door() instanceof WoodenDoor);
assertTrue(house.window() instanceof BambooWindow);
assertTrue(house.roof() instanceof SlateRoof);
}
}
但是我在用 ScalaTest 做同樣的事情時遇到了問題:
class InjectorSpec extends Spec {
describe("An injector") {
it("should inject the correct types") {
val injector = Guice.createInjector(new ModuleImpl)
val house = injector.getInstance(classOf[House])
assert(house.door instanceof WoodenDoor)
assert(house.window instanceof BambooWindow)
assert(house.roof instanceof SlateRoof)
}
}
}
它抱怨值instanceof
不是Door
/ Window
/ Roof
的成員。 我不能在 Scala 中那樣使用instanceof
嗎?
Scala 不是 Java。 Scala 只是沒有操作符instanceof
而是有一個名為isInstanceOf[Type]
的參數化方法。
您可能還喜歡觀看ScalaTest 速成課程。
使用 Scalatest 2.2.x(可能更早),您可以使用:
anInstance mustBe a[SomeClass]
如果您想減少 JUnit 風格,並且想使用 ScalaTest 的匹配器,則可以編寫自己的屬性匹配器來匹配類型(條形擦除)。
我發現這個線程非常有用: http : //groups.google.com/group/scalatest-users/browse_thread/thread/52b75133a5c70786/1440504527566dea?#1440504527566dea
然后,您可以編寫如下斷言:
house.door should be (anInstanceOf[WoodenDoor])
代替
assert(house.door instanceof WoodenDoor)
當前關於 isInstanceOf[Type] 和 junit 建議的答案很好,但我想添加一件事(對於以非 junit 相關身份訪問此頁面的人)。 在許多情況下,scala 模式匹配將滿足您的需求。 在這些情況下,我會推薦它,因為它為您提供免費的類型轉換,並減少出錯的空間。
例子:
OuterType foo = blah
foo match {
case subFoo : SubType => {
subFoo.thingSubTypeDoes // no need to cast, use match variable
}
case subFoo => {
// fallthrough code
}
}
將 Guillaume 的 ScalaTest 討論參考(以及 James Moore 鏈接的另一個討論)合並為兩種方法,針對 ScalaTest 2.x 和 Scala 2.10 進行了更新(使用 ClassTag 而不是清單):
import org.scalatest.matchers._
import scala.reflect._
def ofType[T:ClassTag] = BeMatcher { obj: Any =>
val cls = classTag[T].runtimeClass
MatchResult(
obj.getClass == cls,
obj.toString + " was not an instance of " + cls.toString,
obj.toString + " was an instance of " + cls.toString
)
}
def anInstanceOf[T:ClassTag] = BeMatcher { obj: Any =>
val cls = classTag[T].runtimeClass
MatchResult(
cls.isAssignableFrom(obj.getClass),
obj.getClass.toString + " was not assignable from " + cls.toString,
obj.getClass.toString + " was assignable from " + cls.toString
)
}
我使用 2.11.8 對集合進行斷言。 較新的語法如下:
val scores: Map[String, Int] = Map("Alice" -> 10, "Bob" -> 3, "Cindy" -> 8)
scores shouldBe a[Map[_, _]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.