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;