Skip to content

Instantly share code, notes, and snippets.

@fcanas
Created December 18, 2012 04:02
Show Gist options
  • Save fcanas/4324891 to your computer and use it in GitHub Desktop.
Save fcanas/4324891 to your computer and use it in GitHub Desktop.
Property changes and control events are different. A reminder from testing in Cocoa.

Here's a lesson I learned testing UI components in Cocoa Touch on FCSettingsBooster.

Let's say you have some UISwitch element that you wire up programmatically:

[_theSwitch addTarget:self action:@selector(setDefaults) forControlEvents:UIControlEventValueChanged];

And you're going to try and make sure that some eventual effect is triggered by chaning the UISwitch. The fact that the setDefaults selector is called is irrelevant. In this case, we want to ensure that a completion block is called. You might think you can test it as follows:

__block BOOL probe = NO;
model.valueChanged = ^(BOOL value){
  probe = value;
};

[_theSwitch setOn:YES animated:NO];

probe should be_truthy;

You'd be wrong. The target set up in the code doesn't receive the selector because it never received a UIControlEvent. So you have to trigger it yourself in order to test:

__block BOOL probe = NO;
model.valueChanged = ^(BOOL value){
  probe = value;
};

[_theSwitch setOn:YES animated:NO];
[_theSwitch sendActionsForControlEvents:UIControlEventValueChanged];

probe should be_truthy;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment