2016-04-28 63 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工作。