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

Overview

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

CocoaPods

Just add pod 'Freezer', '~> 1.0' to your test target in Podfile.

Carthage

There is no Xcode project, so Carthage will not build a framework for this library. You can still use it, just add github "Pr0Ger/Freezer" ~> 1.0 to your Cartfile and then add Carthage/Checkout/Freezer/freezer.swift to your test target.

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

You might also like...
Swift framework containing a set of helpful XCTest extensions for writing UI automation tests
Swift framework containing a set of helpful XCTest extensions for writing UI automation tests

AutoMate • AppBuddy • Templates • ModelGenie AutoMate AutoMate is a Swift framework containing a set of helpful XCTest extensions for writing UI autom

Erik is an headless browser based on WebKit. An headless browser allow to run functional tests, to access and manipulate webpages using javascript.
Erik is an headless browser based on WebKit. An headless browser allow to run functional tests, to access and manipulate webpages using javascript.

Erik Erik is a headless browser based on WebKit and HTML parser Kanna. An headless browser allow to run functional tests, to access and manipulate web

Trying to implement Unit Tests for @Binding properties in a ViewModel

BindingTester Trying to implement Unit Tests for @Binding properties in a ViewModel ViewModel to be tested class SheetViewModel: ObservableObject {

Catching fatal errors in unit tests

Precondition Catching When running tests which hit fatal errors, often preconditions the built-in support with XCTest. One package which supports cach

Write unit tests which test the layout of a view in multiple configurations
Write unit tests which test the layout of a view in multiple configurations

Overview This library enables you to write unit tests which test the layout of a view in multiple configurations. It tests the view with different dat

Bluepill is a reliable iOS testing tool that runs UI tests using multiple simulators on a single machine
Bluepill is a reliable iOS testing tool that runs UI tests using multiple simulators on a single machine

Bluepill is a tool to run iOS tests in parallel using multiple simulators. Motivation LinkedIn created Bluepill to run its large iOS test suite in a r

Upload failing iOS snapshot tests cases to S3
Upload failing iOS snapshot tests cases to S3

Second Curtain If you're using the cool FBSnapshotTestCase to test your iOS view logic, awesome! Even better if you have continuous integration, like

Tool for generating Acceptance Tests in Xcode, inspired by Fitnesse
Tool for generating Acceptance Tests in Xcode, inspired by Fitnesse

AcceptanceMark is a tool for generating Acceptance Tests in Xcode, inspired by Fitnesse. Read this blog post for a full introduction to AcceptanceMark

A collection of useful test helpers designed to ease the burden of writing tests for iOS applications.
A collection of useful test helpers designed to ease the burden of writing tests for iOS applications.

MetovaTestKit is a collection of useful test helpers designed to ease the burden of writing tests for iOS applications. Requirements Installation Usag

Comments
  • Support for Swift-Date

    Support for Swift-Date

    I just stumbled upon your Code here, as I'd like to mock my simulators time while doing UI-Testing for AppStore Screenshots. You don't have any working solution for Swift-Date (not NSDate)?

    opened by georgbachmann 0
Owner
Sergey Petrov
Professional pizza consumer and mediocre code writer
Sergey Petrov
Mockit is a Tasty mocking framework for unit tests in Swift 5.0

Mockit Introduction Mockit is a Tasty mocking framework for unit tests in Swift 5.0. It's at an early stage of development, but its current features a

Syed Sabir Salman-Al-Musawi 118 Oct 17, 2022
Boilerplate-free mocking framework for Swift!

Cuckoo Mock your Swift objects! Introduction Cuckoo was created due to lack of a proper Swift mocking framework. We built the DSL to be very similar t

Brightify 1.5k Dec 27, 2022
A convenient mocking framework for Swift

// Mocking let bird = mock(Bird.self) // Stubbing given(bird.getName()).willReturn("Ryan") // Verification verify(bird.fly()).wasCalled() What is Mo

Bird Rides, Inc 545 Jan 5, 2023
A mocking framework for Swift

SwiftMock SwiftMock is a mocking framework for Swift 5.2. Notes on the history of this repo September 2015: first version of this framework November 2

Matthew Flint 257 Dec 14, 2022
Efs - Easy function mocking/stubbing in Swift

Efs Efs, as the pronounced plural of letter F (for function), is a simple mockin

Jérémy Touzy 0 Feb 12, 2022
Mockingbird was designed to simplify software testing, by easily mocking any system using HTTP/HTTPS

Mockingbird Mockingbird was designed to simplify software testing, by easily mocking any system using HTTP/HTTPS, allowing a team to test and develop

FARFETCH 183 Dec 24, 2022
Small library to easily run your tests directly within a Playground

[] (https://developer.apple.com/swift/) Build Status Branch Status master develop About PlaygroundTDD enables you to use TDD directly on Xcode Playgro

Whiskerz AB 317 Nov 22, 2022
PlaygroundTester enables you to easily run tests for your iPad Playgrounds 4 project.

PlaygroundTester PlaygroundTester is a package that enables you to add tests to your iPad Swift Playgrounds project. Installation Just add PlaygroundT

Paweł Łopusiński 36 Dec 13, 2022
UITest helper library for creating readable and maintainable tests

UITestHelper General information When creating UI tests you will see that you are often repeating the same pieces of code. The UITestHelper library wi

Edwin Vermeer 56 Feb 20, 2022
Swift Package with examples of how to tests iOS apps

GimmeTheCodeTDD A swift package with examples of unit tests in iOS development. Requirements Xcode 13 Content Dependency Injection Constructor Injecti

Dominik Hauser 8 Oct 11, 2021