title: AFNetworking Unit Tests
body: <p>I am attempting to unit test using the below code. However, the test simply hangs at the wait portion. My test code is below. I'm new to iOS and to AFNetworking. Any help would be appreciated.</p>
  
  <p>Things I Know: 
  The method in USAPIConnection is being called. 
  There is no success or failure method being called on TestController.
  The request to the rails api is working. (I know it returns a 200 OK code).</p>
  
  <p>Things I Don't Know:
  Is the AFHTTPRequestOperationManager actually sending the request?
  Why is there no success of failure method being sent?
  Should this unit test be sufficient for testing?</p>
  
  <p>XCTest Code</p>
  
  <pre><code>#import &lt;XCTest/XCTest.h&gt;
  #import "USAPIConnection.h"
  #import "Controller.h"
  
  @interface TestController : NSObject &lt;Controller&gt;
  
  @property bool done;
  
  @end
  
  @implementation TestController
  
  -(void)asynchronousSuccessfulWithObject:(id)object type:(int)type{
      NSLog(@"Object: %@", object);
      XCTAssertNotNil(object,"@Should have returned JSON object: %s",__PRETTY_FUNCTION__);
      _done = true;
  
  }
  -(void)asynchronousUnsuccessfulWithError:(NSError *)error type:(int)type{
      XCTFail(@"Unsuccessful async call with error%s: %s",error,__PRETTY_FUNCTION__);
      _done = true;
  }
  
  @end
  
  @interface USAPIConnectionTests : XCTestCase
  
  @end
  
  @implementation USAPIConnectionTests
  
  - (void)setUp
  {
      [super setUp];
      NSLog(@"Beginning Test: %s",__PRETTY_FUNCTION__);
  }
  
  - (void)tearDown
  {
      NSLog(@"Ending Test: %s",__PRETTY_FUNCTION__);
      [super tearDown];
  }
  
  - (void)testGetTeamById
  {
      TestController* testController = [[TestController alloc] init];
      testController.done = false;
      [USAPIConnection getTeamById:1 controller:testController];
      int count = 0;
      while(!testController.done &amp;&amp; count &lt; 10){
          //Waiting
          [NSThread sleepForTimeInterval:3.0];
          count = count + 1;
      }
      if(count == 10){
          XCTFail(@"Timed out: %s",__PRETTY_FUNCTION__);
      }
  }
  
  @end
  </code></pre>
  
  <p>Method Called in USAPIConnection</p>
  
  <pre><code>+(void)getTeamById:(int)identity controller:(id&lt;Controller&gt;)controller{
      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      AFJSONResponseSerializer* serializer = [AFJSONResponseSerializer serializer];
      serializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
      manager.responseSerializer = serializer;
      [manager POST:[NSString stringWithFormat:@"http://%@/team/%D",baseURL,identity] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
          [controller asynchronousSuccessfulWithObject:responseObject type:TYPETEAMSBYNAME];
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          [controller asynchronousUnsuccessfulWithError:error type:TYPETEAMSBYNAME];
      }];
  }
  </code></pre>
  
  <h2>Attempts</h2>
  
  <p>Attempting changing the test method to the below. However, neither NSLog in the test controller ran. </p>
  
  <pre><code>- (void)testGetTeamById
  {
      TestController* testController = [[TestController alloc] init];
      testController.done = false;
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{[USAPIConnection getTeamById:1 controller:testController];});
      //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{NSLog(@"HELLO FROM ASYNC QUEUE");});
  }
  </code></pre>