title: XCTest testing for delegate methods being called
body: <p>I've been trying to test some classes I've created that perform networking actions using the <code>NSNetServer</code> class, among others. I'm having some issues ensuring that the delegate method is called.</p>
  
  <p>I've tried numerous methods, including:</p>
  
  <p>Using <code>[NSThread sleepForTimeInterval:5.0f];</code> and <code>[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0f]];</code> to simply pause whilst the other actions are happening. The <code>NSRunLoop</code> method works the first time it is called (as shown in the example code below), but crashes on the second call. I understand neither are the "correct" way of doing things, but I don't know what the "correct" way is.</p>
  
  <p>Using the <code>NSCondition</code> and <code>NSConditionLock</code> classes, which just seems to lock up the code and the callback is never called.</p>
  
  <p>Using a <code>while</code> loop on a variable changed in the callback method,  same as above.</p>
  
  <p>Below is the code with a couple of extra comments and some of the tests removed for simplicity:</p>
  
  <pre><code>- (void)testCheckCredentials
  {
      [self.server start];
      // Create a client
      self.nsb = [[NSNetServiceBrowser alloc] init];
      // Set the delegate to self
      self.nsb.delegate = self;
      // Search for the server
      [self.nsb searchForServicesOfType:self.protocol inDomain:@""];
      // Wait for the service to be found and resolved
      [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:self.timeout]];
      XCTAssertTrue(self.serviceWasFound, @"Service was not found");
      // Open the connection to the server
      XCTAssertTrue([self.serverConnection open], @"Connection to server failed to open");
      // Wait for the client to connect
      /* This is where it crashes */
      [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:self.timeout]];
      XCTAssertTrue(self.clientDidConnect, @"Client did not connect");
      /* Further, more class-specific tests */
  }
  
  - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)service moreComing:(BOOL)moreComing
  {
      NSLog(@"Found a service: %@ (%@)", service.name, service.domain);
      if ([self.serverName isEqualToString:service.name]) {
          self.serviceWasFound = YES;
      }
  }
  
  - (void)clientDidConnect:(RCFClientConnection *)client
  {
      XCTAssertNotNil(client, @"Connected client is nil");
      self.clientConnection = client;
      self.clientDidConnect = YES;
  }
  </code></pre>
  
  <p>I've also tried doing a <code>lock</code> on an <code>NSCondition</code> object:</p>
  
  <pre><code>[self.nsb searchForServicesOfType:self.protocol inDomain:@""];
  // Wait for the service to be found and resolved
  [self.lock lockWhenCondition:1];
  XCTAssertTrue(self.serviceWasFound, @"Service was not found");
  </code></pre>
  
  <p>and</p>
  
  <pre><code>self.serviceWasFound = YES;
  [self.lock unlockWithCondition:1]
  </code></pre>
  
  <p>When using the lock method, the <code>netServiceBrowser:didFindService:moreComing:</code> method is never called, same when I use:</p>
  
  <p><code>while (!self.serviceWasFound) {};</code></p>
  
  <p>I'm still learning Objective-C but I'm just totally stuck on this problem.</p>