I'm a beginner in Unit Test and I would like to test my cases in a switch but I don't know how to do it.
I have :
- (void)testClickSmiley
{
[self.viewController click:nil];
// Here What i do ? I use what kind of XCTest Assertions ? I want to test if it goes into "default" for example
}
And in my ViewController :
- (IBAction)click:(id)sender
{
UIButton *btn = (UIButton *)sender;
switch (btn.tag) {
case Bad:
// Show view Bad
break;
case Average:
// Show view Average
break;
case Good:
// Show view Bad
break;
default:
break;
}
}
Of course, I don't want to modify my ViewController.
Any ideas ? TY
What you actually should be doing in this case is writing UI tests for this scenario. Your context and execution environment do not allow you to test your code based on unit tests (for example, the app is not aware of any button you pass to the test) the way you expect it.
Of course the first thing that is wrong is that you use
[self.viewController click:nil];
The click
function will get a nil
value for the button and the tag will therefore be nil as well.
Of course you could mock a button:
UIButton *button = [[UIButton alloc] initWith...]
button.tag = [YourEnum].Bad
[self.viewController click: button];
But that would still leave you with the problem that you don't know where the switch ended up going...
Take a look at UI Testing
It allows you to run the application and simulate user interactions + you have the benefit that you can always assume you are working with the actual button that caused the click:
event in the first place.