65111

iPhone testing: testing IBOutlets

I actually feel a little embarrassed asking this because it feels about as basic as you can go, but it's been stumping me for hours...

Let's say I have an instance of a UIViewController (called MyViewController) which simply has one IBOutlet to a UITextField, and when the page loads I either set it to be Enabled or not based on a BOOL property.

- (void)viewDidLoad { [super viewDidLoad]; self.surname.enabled = self.allowTextField; }

Everything is wired up correctly and when I run the app it works as expected (lord - I'd hope so cause if I couldn't get that part right, I probably shouldn't be writing iPhone apps...).

Anyway, I decided to be diligent and write application Unit Tests for the app because it's something I want to become a lot more solid in. Unfortunately, I cannot for the life of me understand why the following does not work...

-(void)testFieldEnabled { MyViewController *myController = [[MyViewController alloc] initWithNibName:nil bundle:nil]; myController.allowTextField = YES; [myController viewDidLoad]; STAssertTrue(myController.surname.enabled, @"Why is this not correct?"); }

I've kept this example extremely simple because I truly don't get why this is not working and believe it must be due a major failing in my understanding of unit testing. Shouldn't the above unit test fire-up an instance of MyViewController page, set the property, run the method and return true? At the moment it keeps telling me that the surname textfield is not enabled.

Thanks for your help and a happy new year to you all!

Answer1:

This is because calling viewDidLoad manually doesn't actually load the view. You should call view property of the view controller, which will load the view automatically (or call loadView manually).

Answer2:

Try the following:

-(void)testFieldEnabled { MyViewController *myController = [[MyViewController alloc] initWithNibName:nil bundle:nil]; [myController view]; myController.allowTextField = YES; STAssertTrue(myController.surname.enabled, @"Why is this not correct?"); }

viewDidLoad will only call your delegate method having your initialization code. It won't do any actual loading of view. Additionally, you need to set allowTextField after loading the view, as it would, otherwise, override it with the default value.

Answer3:

You can use ‘load View’ instead of view did Load. You also have to describe ‘load View’ in My View Controller

Recommend

  • Can I use an http.ServerResponse as a prototype in node.js?
  • Convert XML to/from JSON in Java (without extra and elements)
  • What causes the different behaviors between “var” and “let” when assign them a returned value of a f
  • Specify Source IP for ADO connection from Delphi to MySQL
  • Service detection for .NET Core
  • REGEX to capture sentences with quotes
  • Spring State Machine Error Handling not working
  • Unable to retrieve full text of title from JPEG metadata
  • How to protect an asp:textbox from user input?
  • in batch how do i use taskkill properly
  • Clear activity stack before launching another activity
  • Azure table store snapshot/backup capability
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • How to get current document uri in XSLT?
  • Button text different than value submitted in query string
  • SharedPreferences or SQLite Database?
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • PHP buffered output depending on server setting?
  • iOS: Detect app start via notification press
  • Functions in global context
  • Refering to the class itself from within a class mehod in Objective C
  • Meteor helpers not available in Angular template
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Is calc() supported in html email?
  • What is Eclipse's Declaration View used for?
  • req.body is undefined - nodejs
  • Can Jackson SerializationFeature be overridden per field or class?
  • Is possible to count alias result on mysql
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Check if a string to interpolate provides expected placeholders
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • Large data - storage and query
  • Jquery - Jquery Wysiwyg return html as a string
  • Akka Routing: Reply's send to router ends up as dead letters
  • WOWZA + RTMP + HTML5 Playback?
  • SVN: Merging two branches together
  • Is there a mandatory requirement to switch app.yaml?
  • Append folder name and increment by 1 using batch script