Useful for a few cases but not sure if its possible using Nimble
The reason I am asking this is I want to test a model initializer that sets a default dateCreated: Date but I can't think of a good way to make sure the date got set right. Here is the test:
it("should set default date created") {
let item = Item(name: "", valueInDollars: 0)
expect(item.dateCreated).to(equal(Date()))
}
and here is the model:
class Item: NSObject {
var name: String
var valueInDollars: Int
var serialNumber: String?
let dateCreated: Date
let itemKey: String
init(name: String, serialNumber: String? = nil, valueInDollars: Int, date: Date = Date()) {
self.name = name
self.valueInDollars = valueInDollars
self.serialNumber = serialNumber
self.dateCreated = date
self.itemKey = UUID().uuidString
super.init()
}
}
I am guessing I will have the same issue when I get to the UUID() as well except XCTAssertEqualWithAccuracy wouldn't work in that case...
Use the beCloseTo matcher instead for changing accuracy
@kdawgwilk You can use Nimble's beCloseTo matcher to compare two dates.
An example would be:
- (void)testDateMatches {
NSString *dateStr = @"2015-08-26 11:43:00";
NSString *dateStr2 = @"2015-08-26 11:43:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSDate *date2 = [dateFormat dateFromString:dateStr2];
expect(date).to(beCloseTo(date2).within(10));
}
swift syntax
expect(actual).to(beCloseTo(expected, within: delta))
Most helpful comment
swift syntax
expect(actual).to(beCloseTo(expected, within: delta))