7

I'm trying to use fixture-context objects with async testing in ScalaTest.

The naive approach of simply combining the two doesn't compile. For example:

import org.scalatest.AsyncWordSpec

import scala.collection.GenTraversableOnce
import scala.concurrent.{ExecutionContext, Future}
import scala.math.Numeric.IntIsIntegral

trait Adder[T] {
  implicit def num: Numeric[T]
  def add(number: T): Unit
  def result: Future[T]
}

object Foo {
  def doubleSum[T](adder: Adder[T], numbers: GenTraversableOnce[T])(implicit ec: ExecutionContext): Future[T] = {
    numbers.foreach(adder.add)
    val num = adder.num
    import num._
    adder.result.map(result => result + result)
  }
}

class FooSpec extends AsyncWordSpec {

  trait IntAdder {
    val adder = new Adder[Int] {
      override implicit val num = IntIsIntegral
      private var sum = Future.successful(num.zero)
      override def add(number: Int): Unit = sum = sum.map(_ + number)
      override def result: Future[Int] = sum
    }
  }

  "Testing" should {
    "be productive" in new IntAdder {
      Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
    }
  }
}

This fails to compile with:

Error:(37, 11) type mismatch;
found   : FooSpec.this.IntAdder
required: scala.concurrent.Future[org.scalatest.compatible.Assertion]
          new IntAdder {

This is a legitimate error but I'm wondering what ways there are of solving this in a ScalaTest style.

I want to keep the fixture-context object since that allows me to use the stackable trait pattern.

3 Answers 3

3

What about:

import org.scalatest.compatible.Assertion

class FooSpec extends AsyncWordSpec {

  def withIntAdder(test: Adder[Int] => Future[Assertion]): Future[Assertion] = {
     val adder = new Adder[Int] { ... }
     test(adder)
  }

  "Testing" should {
    "be productive" in withIntAdder { adder =>
      Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
    }
  }
}

Or

class FooSpec extends AsyncWordSpec {

  trait IntAdder {
    val adder = new Adder[Int] {
      override implicit val num = IntIsIntegral
      private var sum = Future.successful(num.zero)
      override def add(number: Int): Unit = sum = sum.map(_ + number)
      override def result: Future[Int] = sum
    }
  }
  trait SomeMoreFixture {

  }

  "Testing" should {
    "be productive" in {
      val fixture = new IntAdder with SomeMoreFixture
      import fixture._
      Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
    }
  }
}
6
  • That is the loan-fixture method pattern. It works but I am more interested in fixture-context objects since you can use the stackable trait pattern and you get all the members in scope automatically without having to declare them all as arguments.
    – steinybot
    Oct 18, 2017 at 19:10
  • I have clarified this requirement in the question.
    – steinybot
    Oct 18, 2017 at 19:15
  • 3
    Not every ScalaTest style support fixture-context objects. Specifically AsyncWordSpec does not. See scaladoc doc.scalatest.org/3.0.0/index.html#org.scalatest.AsyncWordSpec, it's not listed in the "Shared fixtures" section. You might consider using the stackable trait pattern on suite level (see section "Composing fixtures by stacking traits" in scaladoc) and nested suites. Otherwise, I'm afraid you have to implement your own workaround like what you've tried in your own answer.
    – PH88
    Oct 19, 2017 at 3:56
  • Or just create your stackable fixture and then import like my updated answer?
    – PH88
    Oct 19, 2017 at 4:23
  • Good point about it not being in the scaladoc, I thought I had looked there. I like your second solution with the import. It seems to keep all the benefits with minimal overhead.
    – steinybot
    Oct 19, 2017 at 21:21
0

The best solution I have come up with so far is to do something like:

class FooSpec extends AsyncWordSpec {

  trait IntAdder {
    ... // Same as in the question
    val assertion: Future[compatible.Assertion]
  }

  "Testing" should {
    "be productive" in new IntAdder {
      val assertion = Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
    }.assertion
  }
}

I was hoping to reduce it down slightly more into:

class FooSpec extends AsyncWordSpec {

  trait IntAdder extends (() => Future[compatible.Assertion]) {
    ... // Same as in the question
    val assertion: Future[compatible.Assertion]
    override def apply(): Future[Assertion] = assertion
  }

  "Testing" should {
    "be productive" in new IntAdder {
      val assertion = Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
    }()
  }
}

However this also doesn't compile due to:

Error:(42, 10) ';' expected but '(' found.
        }()
0

You could have a mix of the fixture-context objects and the loan-fixtures methods patterns.

Something like this:

class FooSpec extends AsyncWordSpec {

// Fixture-context object 
trait IntAdder {
    val adder = new Adder[Int] {
    override implicit val num = IntIsIntegral
    private var sum = Future.successful(num.zero)
    override def add(number: Int): Unit = sum = sum.map(_ + number)
    override def result: Future[Int] = sum
  }
}

// Loan-fixture method
def withContext(testCode: IntAdder => Future[compatible.Assertion]): Future[compatible.Assertion] = {
  val context = new IntAdder {}
  testCode(context)
}

"Testing" should {
    "be productive" in withContext { context =>
      import context._
      Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.