I write a lot of JavaScript these days and got quite accustomed to Jasmine-style unit tests with their expressiveness and flexibility. I also code a fair amount of Scala and one thing I miss every time I switch is a good support for BDD. I use ScalaTest and it is a powerful framework but one thing it is missing for years is the support for hierarchical specs. Namely, the analog of Jasmine’s “beforeEach” and “afterEach” in nested “describe”s. So recently, after struggling once again to express a test concisely, I felt that enough was enough. Here is a FunSpec extension trait I came up with:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.prystupa | |
import org.scalatest.{Tag, FunSpec} | |
import scala.collection.mutable | |
trait JasmineSpec extends FunSpec { | |
private val itWord = new ItWord | |
private val setup: mutable.Stack[List[() => Unit]] = mutable.Stack() | |
private val tearDown: mutable.Stack[List[() => Unit]] = mutable.Stack() | |
protected def beforeEach(code: => Unit) { | |
val list = (() => code) :: setup.pop() | |
setup.push(list) | |
} | |
protected def afterEach(code: => Unit) { | |
val list = (() => code) :: tearDown.pop() | |
tearDown.push(list) | |
} | |
protected override def describe(description: String)(fun: => Unit) { | |
setup.push(List()) | |
tearDown.push(List()) | |
super.describe(description)(fun) | |
tearDown.pop() | |
setup.pop() | |
} | |
protected override val it = observe | |
private lazy val observe: ItWord = new ItWord { | |
override def apply(specText: String, testTags: Tag*)(testFun: => Unit) { | |
val before = setup.reverse.flatMap(_.reverse) | |
val after = tearDown.flatMap(_.reverse) | |
itWord(specText, testTags: _*) { | |
before.foreach(_()) | |
testFun | |
after.foreach(_()) | |
} | |
} | |
} | |
} |
When mixed in, the trait intercepts FunSpec’s “describe”s to keep track of all the “beforeEach” and “afterEach” on every nesting level. It also overrides FunSpec’s “it” to execute appropriate setup and tear-down code for that “it” before running the test. And here is a contrived example of using it in Scala:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.prystupa | |
import org.scalatest.matchers.ShouldMatchers | |
import org.junit.runner.RunWith | |
import org.scalatest.junit.JUnitRunner | |
/** | |
* Created with IntelliJ IDEA. | |
* User: eprystupa | |
* Date: 8/24/13 | |
* Time: 11:37 PM | |
*/ | |
@RunWith(classOf[JUnitRunner]) | |
class MathSuite extends JasmineSpec with ShouldMatchers { | |
var x: Int = _ | |
var result: Int = _ | |
describe("A simple calculator") { | |
describe("when turned on") { | |
it("displays 0") { | |
result should equal(0) | |
} | |
describe("when I input 5") { | |
beforeEach { | |
x = 5 | |
} | |
describe("and multiple by 3") { | |
beforeEach { | |
result = x * 3 | |
} | |
it("shows 15") { | |
result should equal(15) | |
} | |
} | |
describe("and add by 3") { | |
beforeEach { | |
result = x + 3 | |
} | |
it("shows 8") { | |
result should equal(8) | |
} | |
} | |
} | |
} | |
} | |
} |
For a complete project that you can explore or execute with maven look here: https://github.com/prystupa/jasmine-spec-scala