I'm not sure if I'm approaching this in the correct way, but...
I have a client which, when called, will create a schedule, such as:
def schedule(item: Map[String, String], delay: FiniteDuration): Cancellable = {
val job = ScheduledJob(item, delay)
system.scheduler.scheduleOnce(delay) {
jobRunnerActor ! job
}
}
I'd like to write a test that checks that if this function is called that system.scheduler.scheduleOnce is called with the correct delay.
I've written the following test:
"Orchestration" should {
"schedule a job once using the actor system" in new Actors {
val item = Map(
"type" -> "foo",
"id" -> "bar"
)
val delay = FiniteDuration(1, "second")
// DI the mock ActorSystem
object MockOrchestrator extends Orchestration {
val system = mock[ActorSystem]
}
// stubs
val result: Cancellable = mock[Cancellable]
MockOrchestrator.schedule(any[Map[String, String]], any[FiniteDuration]) returns result
// run the scheduler
MockOrchestrator.schedule(item, delay)
val jobRunnerActor = system.actorOf(Props[JobRunnerActor])
val job = {
jobRunnerActor ! ScheduledJob(item, delay)
}
// system.scheduler.scheduleOnce should be run with the correct delay
there was one(mockSystem.scheduler).scheduleOnce(delay)(job)
}
}
With this test I'm setting up a MockOrchestrator as the 'system' is being passed to this using dependency injection (so I can pass in a mock actor system). Then I'm setting up a stub of the schedule function, running it, then ensuring that the scheduler was called with the delay and the function to run.
However, this isn't running. I think because of the (job) function I'm passing to the check for 'there was one'
Like I say, I'm not sure if this is the correct approach. I don't want to test the Akka scheduler, just that my code creates a proper scheduled job. How should I approach this?
Thanks,
Beth