The best way to build test instruments in ScalaTest

We have test fixtures using a loan template . Use this template to create the “seed data" needed to run a test. when the test is data dependent. after

"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))
    }
  }
}

Where withSubject, withDataSource, withFormType- a test devices return data subject, dataSource, formTyperespectively, from the database. withDataSourceRequired subjectimplicitly. To build DefinitionModelrequires datasource.idand formtype.id. therefore, depending on the data requirements of the test that causes such data construction devices, many nested conditions of attachment are created. Is there a better way to “compose” / structure such fixtures?

+4
source share
1 answer

Tre

traitis your friend. Composition is one of the requirements trait. Very well.

Component Features

From Scala Testable Documents

Stacking fixtures

, , ( ) . ScalaTest, . , , Flexxture , super.withFixture.

, trait s

trait Subject extends SuiteMixin { this: Suite =>

  val subject = "Some Subject"

  abstract override def withFixture(test: NoArgTest) = {
    try super.withFixture(test) // To be stackable, must call super.withFixture
    // finally clear the context if necessary, clear buffers, close resources, etc.
  }
}

trait FormData extends SuiteMixin { this: Suite =>

  val formData = ...

  abstract override def withFixture(test: NoArgTest) = {
    try super.withFixture(test) // To be stackable, must call super.withFixture
    // finally clear the context if necessary, clear buffers, close resources, etc.
  }
}

trait , :

class ExampleSpec extends FlatSpec with FormData with Subject {

    "save definition" should {
        "create a new record" in {

            // use subject and formData here in the test logic            

        }
    }

}

Stackable Traits Pattern

+2
source

All Articles