Reputation: 6132
I'm writing my first Funspec
and want to use the same fixture for several tests, but still want to use a new instance on each one. For that I use the BeforeAndAfter
trait. My problem is I don't know how to refer initialization of the object under test to the before
method and still store it in a val
to make it final. The current looks like:
class CarTest extends FunsSpec with BeforeAndAfter{
var car:Car = _
before {
car = Car("BMW i3")
}
describe("A car") {
it("can accelerate") { //do some stuff with car }
it("can break") { //do some other stuff with car}
}
}
So, the question is: Is there some way to make car
a val
and still get it initialized before each method with ScalaTest
?
Upvotes: 2
Views: 1698
Reputation: 35463
You could try using a fixture like so:
import org.scalatest.FunSpec
import org.scalatest.BeforeAndAfter
import org.scalatest.fixture
case class Car(model:String)
class CarTest extends fixture.FunSpec{
type FixtureParam = Car
def withFixture(test: OneArgTest) {
val car = Car("BMW")
test(car)
}
describe("A car") {
it("can accelerate") { car =>
}
it("can break") { car =>
}
}
}
Upvotes: 4