Search code examples
scalascalatest

ScalaTest - how can I find out how many times a method inside an eventually block was executed?


I'm using ScalaTest's Eventually.eventually and have configured it so that it tries to get a record every second until the status is complete:

Eventually.eventually(timeout(5 seconds), interval(1 seconds)) {
   getItem(recordId).record.forall(
      _.state.contains(Constants.RecordStatusComplete)) shouldEqual true
}

I must get the number of times getItem(recordId) was called. How can I do that?


Solution

  • How about just counting it? A bit ugly maybe but scalatest doesn't seem to provide you the amount of attempts it made and presumably this is for testing only, so:

    val count = Iterator from 0
    Eventually.eventually(timeout(5 seconds), interval(1 seconds)) {
       count.next()
       getItem(recordId).record.forall(
          _.state.contains(Constants.RecordStatusComplete)) shouldEqual true
    }
    // count.next() will give you the attempts
    

    I could think of other solutions using mocking frameworks etc. but they'll probably turn out fairly similar.