Mys*_*gue 6 scala fixtures scalatest
当前,我有一组共享的Scalatest测试,这些测试生活在特征中,并在必要时混合到测试类中,如下所示:
trait SharedBehavior{
def supportsCoolFeature(){
//Testing happens here
}
def doesNotSupportCoolFeature(){
//Testing happens here
}
}
Run Code Online (Sandbox Code Playgroud)
class MyItemTests extends SharedBehavior{
"A-Type Items" should behave like supportsCoolFeature(itemA)
"B-Type Items" should behave like doesNotSupportCoolFeature(itemB)
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,itemA
和itemB
是指生活在数据库中的对象Scala的类实例。要运行这些测试,我需要创建相应的数据库记录,然后在测试后将其删除。如何有效地使用夹具策略,使我能够通过共享测试执行设置和拆卸?
尽管这不是理想的选择,但我还是愿意接受一个解决方案,该解决方案只对整个套件进行一次设置和拆卸。我可以编写测试,以便它们不会互相干扰,但是我需要防止数据库随着时间的推移变得混乱。
尝试
class MyItemTests extends SharedBehavior with BeforeAndAfterEach with DbSupport {
override def afterEach(): Unit = {
db.deleteAll()
}
def itemA: Item = {
val item = createItemA
db.insert(item)
item
}
def itemB: Item = {
val item = createItemB
db.insert(item)
item
}
"A-Type Items" should behave like supportsCoolFeature(itemA)
"B-Type Items" should behave like doesNotSupportCoolFeature(itemB)
}
trait SharedBehavior {
def supportsCoolFeature(item: => Item) {
// assert on item
}
def doesNotSupportCoolFeature(item: => Item) {
// assert on item
}
}
trait DbSupport {
val db = {
// initialise dataase
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
55 次 |
最近记录: |