scala - Freeze time during test, Mock singleton objects -


i'm trying freeze clock during specs execution. timecop scala/play 2.4

it's related mocking singleton objects since able specify return value zoneddatetime.now().

my advice inject interface providing now method method needing know current time. can use mockito , specs2-mock return time want

import org.specs2._, mock._  class timespec extends specification mockito { def = s2"""      test event creation specific time $time1    """    def time1 = {     val time = mock[datetime]     val timestamp = new datetime("2016-01-30 12:00:00")       time.now returns timestamp      val event = event.create(time)     event.dt === timestamp   } }  trait time {   def now: datetime }  case class event(dt: datetime)  object event {   def create(time: datetime): event =      event(time.now) } 

Comments