Newbie question: Setting UITextField text programatically in test context

Discussion in 'iOS Programming' started by doubleminus, Nov 20, 2011.

  1. doubleminus, Nov 20, 2011
    Last edited: Dec 27, 2011
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Perhaps start off by testing your assumption that test_subject.textField isn't nil.
     
  3. doubleminus, Nov 20, 2011
    Last edited: Dec 27, 2011

    doubleminus thread starter macrumors newbie

    Joined:
    Nov 3, 2011
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    Actually I was saying that you've missed a step. You've tested that test_subject isn't nil. But then you've gone on to send setText: to test_subject.textField without first testing that test_subject.textField isn't nil.

    Code:
        test_subject = [[ViewController alloc] init];
        (void) [test_subject view];
    
        STAssertNotNil(test_subject, @"Could not create test subject.");
        (void) [test_subject view];
    
        [COLOR=red]STAssertNotNil(test_subject.textField, @"Text field is nil.");[/COLOR]
    
        [test_subject.textField setText: @"30"];
        NSLog(@"$$$  currentage early: = %@", [test_subject.textField text]);
    
     
  5. doubleminus, Nov 21, 2011
    Last edited: Dec 27, 2011

    doubleminus thread starter macrumors newbie

    Joined:
    Nov 3, 2011
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    What are the usual reasons for an instance variable to be nil?

    The first and most obvious one is that it's uninitialized. That is, an object hasn't yet been stored there.

    So look in the init method (or methods) of your class, and see if you're creating an object and storing it in the instance variable. Repeat this for every instance variable your class has.

    If you don't understand the difference between a variable and what it holds, you'll need to review that. Also the difference between declaring a variable and assigning it a value.

    Since you declared your variable as:
    Code:
    @property (weak, nonatomic) IBOutlet UITextField *textField;
    
    I presume you intend to use a nib with the class. Otherwise there's no point in using the word IBOutlet. So you should also look at how IBOutlets are actually initialized.

    And if you're learning from a book or tutorial, please post exactly which one (title, author, version), and exactly where you are in it (page #), and whether this test program is something the book directed you to do, or whether it's your own independent undertaking.
     
  7. doubleminus, Nov 21, 2011
    Last edited: Dec 27, 2011

    doubleminus thread starter macrumors newbie

    Joined:
    Nov 3, 2011
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    Summary: You completed one tutorial successfully. You then started modifying the tutorial material, without going through any other tutorial or instructional materials.

    What is your previous experience in programming? Be specific.

    I don't know what this means. You've only posted partial code of one method. Is that a "test" or not? How are these tests run? Do you mean you're creating unit tests or some other kind of tests?

    Show your code. Ideally, show enough code that someone else can run it. Obviously, that would include nibs, project file, and source code.

    Even better, consider getting a book that contains more instructional material than a single tutorial.

    If you can't figure out how to debug your own code, and you haven't posted it somewhere for anyone else to see, then you're probably in over your head and you should take a more measured approach. Like getting a book.
     
  9. doubleminus, Dec 3, 2011
    Last edited: Dec 27, 2011

    doubleminus thread starter macrumors newbie

    Joined:
    Nov 3, 2011
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    It's not that I'm offended. It's that I can't answer your question with so little information. And I don't think anyone else can either. That's why I asked for more code. Because what you've posted isn't enough to diagnose anything. It's not enough to run a test of anything either.

    As to modifying the tutorial, I just wanted to know your general programming experience. Some people come here and say "I'm a noob", but it turns out they have extensive experience in other languages, and are only a noob to Objective-C and Cocoa. I was especially asking because most noob programmers don't use assertions, or even know what they are.

    If you've only done the single tutorial, then it's even more important that we see the entirety of what you've done, if you expect anyone here to be able to help fix it. I have no other useful suggestions about debugging code without seeing it.
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    Learning unit tests on the first day is mighty strange.

    Properties are likely to be nil because they haven't been initialized correctly. However, you say the app works correctly. If working correctly depends on this property being initialized then there is a contradiction. You need to debug this.

    What are the values of your properties inside viewDidLoad?

    Is viewDidLoad called when you poke the view property, as it should be?

    Is it possible that the nib isn't in the unit test target?

    I personally would recommend that you leave unit tests alone until after you've finished your first book on iPhone development, at least.
     

Share This Page