Skip to content

Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

PlatformGitHub issuesGitHub licenseCode of Conduct

DateKit

DateKit gives you a handy way to provide the current date to your iOS app, while simultaneously making it quick and easy to mock dates in your tests.

In production code, you simply call DateKit.now() to get the current date, instead of using Date() or NSDate().

Then, in test code, you can mock the current date by calling DateKit.mockCurrentDate(as:), and passing in a string of the format "yyyy-MM-dd HH:mm:ss". At this point, DateKit.now() will return the mocked date/time instead of the actual date/time.

DateKit also provides a helper method — getDate(as:) — for creating standalone dates. This is a simple wrapper to make it easier and more succinct to create a new date object.

DateKitUI

DateKitUI extends the functionality of DateKit by giving you full control of mocked dates in your Xcode UI tests. Since you can't access the inner workings of your app during UI testing in the same way that you would during a unit test, DateKitUI employs a nearly-hidden UITextField element to allow you to enter date-strings via the UI instead.

Installation

Drag the DateKit directory into your Xcode project.

DateKit Usage Example

In your production code, use DateKit.now() anywhere you would normally use Date() or NSDate(), such as in this contrived code sample:

account.activationDate =DateKit.now()

Now, in your unit tests, you can control what DateKit.now() returns by using DateKit.mockCurrentDate(as:)—which means you can effectively move your tests forward in time to make sure that time/date-critical code works the way you intended.

func testUserMembershipYears_givenOneYear_returnsCorrectCount(){DateKit.mockCurrentDate(as:"2017/11/17 23:59:59")lettestUser=AccountManager.initializeNewAccount()DateKit.mockCurrentDate(as:"2018/11/28 03:21:18")letactual= testUser.membershipYears
letexpected=1XCTAssertEqual(expected, actual)}

Should you need, you can mock other dates as well, using DateKit.getDate(as:)—like I do here to test that my numDaysElapsed(from:to:) method is functioning correctly.

func testNumDaysElapsed_givenSameDate_returnsZero(){varbegin=DateKit.getDate(as:"2018-08-09 00:30:30")varend=DateKit.getDate(as:"2018-08-09 05:22:18")varexpected=0varactual=numDaysElapsed(from: begin, to: end)XCTAssertEqual(expected, actual)}

DateKitUI Usage Example

Using DateKitUI in your UI tests is fairly straightforward—first, choose a UIViewController where you'll instantiate and interact with the hidden UITextField.

overridefunc viewDidLoad(){
super.viewDidLoad()DateKit.enableUITests(view: view)}

Now, in XCTestCase, you can setup your UI tests like this:

overridefunc setUp(){
super.setUp()
app.launch()
date = app.textFields["DateKitUI"]}

Changing the date in your UI tests looks like this:

date.set("1995/1/1 00:00:00")

And if you like, you can use this handy extension to set the date, close the app, then relaunch it—all in one step:

app.launchTo("2000/1/3 01:02:00")

That's it! I hope you find DateKit as useful as I have. Now get out there and test, test, test!

Contact

License

Distributed under the MIT license. See LICENSE for more information.

Thanks for stopping by!

About

A custom Swift class for easily mocking and testing dates and times in iOS apps. Works with both unit tests and UI tests.

Topics

Resources

Code of conduct

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages