Nimble: Is there a Nimble equivalent to XCTAssertEqualWithAccuracy

Created on 9 Oct 2016  路  4Comments  路  Source: Quick/Nimble

Useful for a few cases but not sure if its possible using Nimble

Most helpful comment

swift syntax

expect(actual).to(beCloseTo(expected, within: delta))

All 4 comments

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))

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ScottRobbins picture ScottRobbins  路  7Comments

hamchapman picture hamchapman  路  7Comments

Blackjacx picture Blackjacx  路  7Comments

ikesyo picture ikesyo  路  4Comments

darrarski picture darrarski  路  4Comments