Simple test with Scala Test 1.2
I'm trying to run a test using Scala 2.8 in IntelliJ 10 with Scala Test 1.2
I don't know much scala syntax, so can you help with this code ? :)
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)
}
}
}
The error is :
error: value in is not a member of java.lang.String
"A Button Observer should observe button clicks" in {
Here is the rest of the code :
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
}
}
The code is from the book "Programming Scala" from Oreilly
I tried updating the code to Scala test 1.2, but failed. Here is the original test (notice the imports and the extended class):
package ui
import org.specs._
import observer._
object Butt开发者_运维技巧onObserverSpec 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
}
}
}
Trial number 3 (Error is No tests were found) :
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)
}
}
}
}
Working test :
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 : If you don't know how to run scala tests in IntelliJ, see this blog post - IntelliJ Now Supports ScalaTest
I think you need to extend WordSpec
with ShouldMatchers
like this :
class ButtonObserverSpec extends WordSpec with ShouldMatchers {...}
You can find more examples here:
http://www.scalatest.org/getting_started_with_bdd
精彩评论