CocoaPods trunk is moving to be read-only. Read more on the blog, there are 11 months to go.

Freezer 1.0.0

Freezer 1.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release May 2016
SPMSupports SPM

Maintained by Sergey Petrov.



Freezer 1.0.0

Freezer

Freezer is a library that allows your Swift tests to travel through time by mocking NSDate class.

Usage

Once Freezer.start() has been invoked, all calls to NSDate() or NSDate(timeIntervalSinceNow: secs) will return the time that has been frozen.

Helper function

freeze(NSDate(timeIntervalSince1970: 946684800)) {
    print(NSDate()) // 2000-01-01 00:00:00 +0000
}

Raw usage

let freezer = Freezer(to: NSDate(timeIntervalSince1970: 946684800))
freezer.start()
print(NSDate()) // 2000-01-01 00:00:00 +0000
freezer.stop()

Time shifting

Freezer will move you to a specified point in time, but then the time will keep ticking.

timeshift(NSDate(timeIntervalSince1970: 946684800)) {
    print(NSDate()) // 2000-01-01 00:00:00 +0000
    sleep(2)
    print(NSDate()) // 2000-01-01 00:00:02 +0000
}

Nested calls

Freezer allows performing nested freezing/shifts

freeze(NSDate(timeIntervalSince1970: 946684800)) {
    freeze(NSDate(timeIntervalSince1970: 946684000)) {
        freeze(NSDate(timeIntervalSince1970: 946684800)) {
            print(NSDate()) // 2000-01-01 00:00:00 +0000
        }
        print(NSDate()) // 1999-12-31 23:46:40 +0000
    }
    print(NSDate()) // 2000-01-01 00:00:00 +0000
}

Installation

Manual

Just copy freezer.swift to your Xcode project and add it to your tests target. Most likely this library will not be updated, unless Apple breaks something by changing an internal implementation of NSDate, so this way is good too.

License

MIT