2016-04-28 90 views
0

使用ScalaTest時出現奇怪的錯誤。 我有以下代碼:使用ScalaTest編譯錯誤?

import org.scalatest.junit.JUnit3Suite; 
import Element.elem; 

class ElementSuite extends JUnit3Suite { 

    def testD() { 
    val ele = elem('x', 2, 3); 
    assert(ele.width === 2); 
    } 

} 

我得到一個編譯錯誤說「非法繼承; selftype ElementSuite不符合org.scalatest.junit.JUnit3Suite的selftype org.scalatest.junit.JUnit3Suite」

有什麼想法?

注意我把這個例子直接從馬丁Oderskey的書,所以應該很好地工作......

+0

不知道,但我不認爲JUnit3Suite被廣泛使用,自從Odersky寫他的書以來,Scalatest已經經歷了幾個版本。我一直使用Funsuite - 請試試? –

回答

1

很難說究竟有哪些問題是不知道你是哪個斯卡拉, SBT,Scalatest等版本使用,但這可以使用更新的版本。 與其試圖準確確定與舊版本的斷開連接的位置,我認爲在當前版本中您可以更輕鬆一些。

build.sbt:

resolvers ++= Seq(
    "Sonatype releases" at "https://oss.sonatype.org/content/repositories/releases" 
) 
libraryDependencies += "org.scalactic" %% "scalactic" % "2.2.6" 
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.6" % "test" 

測試類:

import org.scalatest.FunSuite 

// Random implementation because I'm not sure what Martin's elem class is. 
case class elem(someField: Char, width: Int, height: Int) 

class ElemSuite extends FunSuite { 
    test("D") { 
    val ele = elem('x', 2, 3) 
     assert(ele.width === 2) 
    } 
} 

我覺得這是你會得到書中的代碼最接近的匹配。不過,我喜歡使用FlatSpec和FeatureSpec與匹配器寫我的測試時沿:

import org.scalatest.{FlatSpec, Matchers} 

class ElemSpec extends FlatSpec with Matchers 
{ 
    it should "retrieve the correct width" in { 
    val ele = elem('x', 2, 3) 
    ele.width shouldBe 2 
    } 
} 

不過,如果你是新來斯卡拉,你可能會堅持的風格, 書相匹配的更好。

上ScalaTest風格的更多信息:http://www.scalatest.org/user_guide/selecting_a_style

免責聲明:我其實是採用了最新的3.0.0發佈候選,但我 認爲這些簡單的測試仍然會使用你的2.2.6工作。