Skip to content

Instantly share code, notes, and snippets.

@syshen
Created March 3, 2015 09:51
Show Gist options
  • Select an option

  • Save syshen/47069b6170aa9052b2f2 to your computer and use it in GitHub Desktop.

Select an option

Save syshen/47069b6170aa9052b2f2 to your computer and use it in GitHub Desktop.
- (void)testNormalReturn {
// Create mock objects for API clients
id HNClientMock = OCMPartialMock([HNClient sharedClient]);
id RAClientMock = OCMPartialMock([ReadabilityClient sharedClient]);
// Stub the API responses
NSDictionary *HNRtn = @{@"by":@"andrewbarba",
@"descendants":@12,
@"id":@9135641,
@"kids":@[@9135772,@9135785],
@"score":@35,
@"text":@"",
@"time":@1425346972,
@"title":@"Io.js is one step closer to ARM64",
@"type":@"story",
@"url":@"https://github.com/iojs/io.js/blob/v1.x/CHANGELOG.md#2015-03-02-version-143-rvagg"
};
OCMStub([HNClientMock itemWithIdentity:1000]).andReturn([RACSignal return:HNRtn]);
NSDictionary *RARtn = @{
@"domain": @"patrick.georgi-clan.de",
@"next_page_id": [NSNull null],
@"url": @"http://patrick.georgi-clan.de/2015/02/17/intel-boot-guard/",
@"short_url": @"http://rdd.me/miee7j7a",
@"author": [NSNull null],
@"excerpt": @"<<excerpt>>",
@"direction": @"ltr",
@"word_count": @1118,
@"total_pages": @0,
@"content": @"a lot of content here",
@"date_published": @"2015-02-17 15:04:35",
@"dek": [NSNull null],
@"lead_image_url": [NSNull null],
@"title": @"Intel Boot Guard",
@"rendered_pages": @1};
OCMStub([RAClientMock contentForUrl:[OCMArg any]]).andReturn([RACSignal return:RARtn]);
// Setup the expectation for async test
XCTestExpectation *expectation = [self expectationWithDescription:@"Content is loaded"];
StoryViewModel *viewModel = [[StoryViewModel alloc] initWithIdentity:1000];
[[viewModel.loadContentCommand.executionSignals flatten]
subscribeNext:^(id x) {
// Verify the execution results
XCTAssert(viewModel.content, @"Readability content is not set");
XCTAssert(([viewModel.content isEqualToString:@"<<excerpt>>"]), @"Readability response is not expected");
// Since loadContentCommand is the last to be called, we need to fulfill the expectation
[expectation fulfill];
}];
[[viewModel.loadStoryCommand execute:nil] subscribeNext:^(id x) {
XCTAssert([viewModel.title isEqualToString:@"Io.js is one step closer to ARM64"], @"HN API response is not expected");
XCTAssert([viewModel.date isEqualToString:@"09:42 03/03"], @"The date time format is wrong");
XCTAssert([viewModel.url isEqualToString:HNRtn[@"url"]], @"URL is not expected");
} error:^(NSError *error) {
[expectation fulfill];
XCTFail(@"Cannot be error");
}];
// Wait for 5 seconds timeout to validate async code
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment