iOS Newbie question: Setting UITextField text programatically in test context

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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]);
 

chown33

Moderator
Staff member
Aug 9, 2009
8,774
5,150
vertical
Any idea of why the text field itself would be null? And how to fix that? : )
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,774
5,150
vertical
This all makes perfect sense. I'm learning from the official Apple beginner's tutorial, but elaborated a bit after finishing it to use it for my own little app. Tutorial is here: http://developer.apple.com/library/...ptual/iPhone101/Articles/00_Introduction.html
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 am using it with a nib. And I will say that everything works perfectly in using the app. I just have all of these problems in setting up tests.
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?

So now I am trying to use initWithNibName but with no luck either. Or should I be trying to initialize each UI component separately?
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,774
5,150
vertical
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.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.