Skip to content

Instantly share code, notes, and snippets.

@balopat
Created October 30, 2013 04:21
Show Gist options
  • Save balopat/7227169 to your computer and use it in GitHub Desktop.
Save balopat/7227169 to your computer and use it in GitHub Desktop.
package com.balopat.timeinjection
import org.scalatest.FlatSpec
import java.util.Date
import org.scalatest.matchers.ShouldMatchers
class WallSpec extends FlatSpec with ShouldMatchers {
class MockedClock(var time: Date) extends Clock {
def getTime() = time
}
val mockedClock = new MockedClock(new Date())
"messages" should "be written in reverse order on the wall" in {
val wall = new Wall(mockedClock) //we are injecting our mocked time
val timeAtFirst = new Date()
mockedClock.time = timeAtFirst //we are controlling time!
wall.message("Hello World!")
val timeAtSecond = new Date()
mockedClock.time = timeAtSecond//we are controlling time!
wall.message("Hello, HelloWorld!")
wall.messages should equal(
List(Message("Hello, HelloWorld!", timeAtSecond),
Message("Hello World!", timeAtFirst))
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment