我正在尝试使用Scala测试1.2在IntelliJ 10中使用Scala 2.8进行测试
我不太了解scala语法,所以你可以帮助这段代码吗? :)
package test.ui {
import org.scalatest._
import matchers.ShouldMatchers
import ui._
import observer._
object ButtonObserverSpecification extends Spec {
"A Button Observer should observe button clicks" in {
val observableButton = new ObservableButton("Okay")
val buttonObserver = new ButtonCountObserver
observableButton.addObserver(buttonObserver)
for (i <- 1 to 3) observableButton.click()
buttonObserver.count should equal (3)
}
}
}
错误是:
error: value in is not a member of java.lang.String
"A Button Observer should observe button clicks" in {
以下是代码的其余部分:
package ui {
abstract class Widget
class Button(val label: String) extends Widget {
def click() = {
println (label + " was clicked")
}
}
}
package observer {
trait Subject {
type Observer = {def receiveUpdate(subject: Any)}
private var observers = List[Observer]()
def addObserver(observer: Observer) = observers ::= observer
def notifyObservers = observers foreach (_.receiveUpdate(this))
}
}
package ui {
import observer._
class ObservableButton(name: String) extends Button(name) with Subject {
override def click() = {
super.click()
notifyObservers
}
}
class ButtonCountObserver {
var count = 0
def receiveUpdate(subject: Any) = count += 1
}
}
代码来自Oreilly的“Programming Scala”一书
我尝试将代码更新为Scala test 1.2,但失败了。这是原始测试(注意导入和扩展类):
package ui
import org.specs._
import observer._
object ButtonObserverSpec extends Specification {
"A Button Observer" should {
"observe button clicks" in {
val observableButton = new ObservableButton("Okay")
val buttonObserver = new ButtonCountObserver
observableButton.addObserver(buttonObserver)
for (i <- 1 to 3) observableButton.click()
buttonObserver.count mustEqual 3
}
}
}
试用版3 (错误是未找到测试):
package test.ui {
import org.scalatest._
import matchers.ShouldMatchers
import ui._
import observer._
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
@RunWith(classOf[JUnitRunner])
object ButtonObserverTest extends WordSpec with ShouldMatchers {
"A Button Observer" should {
"observe button clicks" in {
val observableButton = new ObservableButton("Okay")
val buttonObserver = new ButtonCountObserver
observableButton.addObserver(buttonObserver)
for (i <- 1 to 3) observableButton.click()
buttonObserver.count should equal (3)
}
}
}
}
工作测试:
package test.ui {
import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import ui._
import observer._
class ButtonObserverTest extends WordSpec with ShouldMatchers {
"A Button Observer" should {
"observe button clicks" in {
val observableButton = new ObservableButton("Okay")
val buttonObserver = new ButtonCountObserver
observableButton.addObserver(buttonObserver)
for (i <- 1 to 3) observableButton.click()
buttonObserver.count should equal (3)
}
}
}
}
PS:如果您不知道如何在IntelliJ中运行scala测试,请参阅this blog post - IntelliJ Now Supports ScalaTest
答案 0 :(得分:4)
我认为你需要像WordSpec
这样扩展ShouldMatchers
:
class ButtonObserverSpec extends WordSpec with ShouldMatchers {...}
您可以在此处找到更多示例: