Search code examples
scalascalatestfixtures

How to use loan-fixture method with AsyncFeatureSpec?


How can I use loan-fixture method with AsyncFeatureSpec? I have tried as the following:

import akka.actor._
import akka.testkit._
import com.sweetsoft._
import org.scalatest._

import scala.concurrent.duration._

class SapOnlineKafkaOnlineSpec extends fixture.AsyncFeatureSpec with Matchers
  with GivenWhenThen
  with BeforeAndAfter
  with BeforeAndAfterAll {
  private val listener1 = TestProbe()
  private val detector = system.actorOf(DetectorSupervisor.props)

  def withKafkaAndSap(testCode: (ActorRef) => Any) {



  }


  feature("Detect Kafka and SAP availability") {
    info("As a technical user, I want to be notified in real time, if Kafka and SAP is up and running or not.")
    scenario("SAP and Kafka are available") in withKafkaAndSap { (listener) =>
      Given("I am waiting for the current state message")
      detector ! AddNewListener(listener1.ref)
      When("I am receive the state message")
      val res1 = listener1.expectMsgPF[Assertion](6.second) _
      Then("it should contain `SAP and Kafka are online`")
      res1 {
        case status: ServerStatus =>
          status.health should be(ServersOnline)
      }
    }
  }
}

I do not know, how to inject the loan method.


Solution

  • Try the following

    class SapOnlineKafkaOnlineSpec extends AsyncFeatureSpec with Matchers
      with GivenWhenThen
      with BeforeAndAfter
      with BeforeAndAfterAll {
    
      def withKafkaAndSap(testCode: ActorRef => Future[Assertion]) = {
        val actor = ...
        testCode(actor)
      }
    
      feature("Detect Kafka and SAP availability") {
        info("As a technical user, I want to be notified in real time, if Kafka and SAP is up and running or not.")
        scenario("SAP and Kafka are available") {
          withKafkaAndSap { listner =>
            Given("I am waiting for the current state message")
            When("I am receive the state message")
            Then("it should contain `SAP and Kafka are online`")
            succeed
          }
        }
      }
    }
    

    Note the loan fixture parameter type should be

    testCode: ActorRef => Future[Assertion]
    

    instead of ActorRef => Any, and we extend just AsyncFeatureSpec instead of fixture.AsyncFeatureSpec