gpt4 book ai didi

scala - 在 ScalaTest 中组合测试装置的更好方法

转载 作者:行者123 更新时间:2023-12-04 14:43:49 25 4
gpt4 key购买 nike

我们有使用 loan pattern 的测试装置.利用此模式创建运行测试所需的“种子数据”。当测试依赖于数据例如下列的

"save definition" should {
"create a new record" in withSubject { implicit subject =>
withDataSource { implicit datasource =>
withFormType { implicit formtype =>

val definitn = DefinitionModel(-1, datasource.id, formtype.id, subject.role.id, Some(properties))
}
}
}

哪里 withSubject , withDataSource , withFormType测试夹具是否返回 subject , dataSource , formType数据分别来自数据库。 withDataSource夹具需要 subject含蓄地。大厦 DefinitionModel需要 datasource.idformtype.id .因此,根据调用此类数据生成器固定装置的测试的数据要求,会产生大量嵌套固定装置的情况。有没有更好的方法来“组合”/构建这样的装置?

最佳答案

@Nader Hadji Ghanbari 给出的答案仍然成立。我只想补充一点,自 scalatest 3.x.x 版以来,特征更改了名称。从迁移指南复制:

Mixin traits that override withFixture

4) In 3.0.0, the withFixture method has been moved from Suite to a new trait, TestSuite. This was done to make room for a withFixture method with a different signature in AsyncTestSuite. If you factored out a withFixture method into a separate "suite mixin" trait, you'll need to change "Suite" to "TestSuite" and "SuiteMixin" to "TestSuiteMixin". For example, given this trait from 2.2.6:

trait YourMixinTrait extends SuiteMixin { this: Suite =>
abstract override def withFixture(test: NoArgTest): Outcome = {
// ...
}
}

You will need to add the "Test" prefix, like this:

trait YourMixinTrait extends TestSuiteMixin { this: TestSuite =>
abstract override def withFixture(test: NoArgTest): Outcome = {
// ...
}
}

关于scala - 在 ScalaTest 中组合测试装置的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28845975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com