jeudi 19 février 2015

Unit testing completion blocks in iOS

I am trying to figure out how to write unit test for client backend code but I have not been successful so far. I am not so experience with unit testing so I read This, This, andThis


and read some similar questions to what I am asking, but I have not been completely figure out how I should be writing unit tests for the client backend.



- (void)testThatItGetsAllUsers
{
// given
XCTestExpectation *expectation = [self expectationWithDescription:@" fetch all users"];

// when
[[Backend sharedInstance] getAllUsers:^(NSArray * arr) {

XCTAssertNotNil(arr);
[expectation fulfill];

}andFailure:^(NSError * err) {
XCTAssert((err != nil), @"get all users request failed with error:\t%@", err);

}];

[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
// request not successfull
XCTAssert((error != nil), @"get all users from server did not return any data:\t%@", error);
}];
// then
}


It would be really helpful if someone can point out what I am doing wrong and any additional knowledge is appreciated!


Aucun commentaire:

Enregistrer un commentaire