将班级成员带到外部班级或特质的便捷方式?

时间:2017-04-01 02:04:25

标签: java scala scalatest

在我的TestSpec类中,我有很多属性,它开始变得拥挤,类似于:

implicit val formats: Formats = DefaultFormats

  val client: WSClient = app.injector.instanceOf[WSClient]
  val env: Environment = app.injector.instanceOf[Environment]
  val config: Configuration = app.configuration
  val j4s: Json4s = app.injector.instanceOf[Json4s]
  val actSys: ActorSystem = app.injector.instanceOf[ActorSystem]
  implicit lazy val materializer: Materializer = app.materializer

  // mocking service
  val myService: ChironApiService = mock[MyService]

  val rawTextModel: String =
    """{"message":"a message","reason":"Done","list":[34,2,12]}"""
  val badResponseModel: ModelOne = JsonMethods.parse(rawTextModel).extract[BadResModel]

  val futureOfEither: Future[Either[String, Int]] = Future(Left("fr"))
.
.
.

所以我想把它带到一个特性并将这个特性添加到TestSpec然后我可以访问这些信息,是否有更优雅的scala方式来做这样的事情?

感谢

1 个答案:

答案 0 :(得分:0)

您可以尝试类似

的内容
trait TestConfig {
    val client: WSClient = app.injector.instanceOf[WSClient]
    val env: Environment = app.injector.instanceOf[Environment]
    val config: Configuration = app.configuration
    val j4s: Json4s = app.injector.instanceOf[Json4s]
    val actSys: ActorSystem = app.injector.instanceOf[ActorSystem]
    implicit lazy val materializer: Materializer = app.materializer
 }

 class TestSpec extends TestConfig {
    // mocking service
    val myService: ChironApiService = mock[MyService]

    val rawTextModel: String =
        """{"message":"a message","reason":"Done","list":[34,2,12]}"""
    val badResponseModel: ModelOne = JsonMethods.parse(rawTextModel).extract[BadResModel]

    val futureOfEither: Future[Either[String, Int]] = Future(Left("fr"))
    .
    .
    .
 }