Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:18:29+00:00 2026-06-13T01:18:29+00:00

I am writing a unit test template project for my own reference’s sake. This

  • 0

I am writing a unit test template project for my own reference’s sake. This includes template code for all the basic tasks, such as verifying that a UILabel’s text property is set as expected after viewDidLoad.

I have a storyboard with a ViewController with one UILabel. In viewDidLoad I set it’s text. The test then asserts whether or not the text has been set as expected, but for some reason, the UILabel is always nil. Everything works as expected (thank god) when I run the app; a white view is shown with a label with the expected text.

I am using Kiwi as my test framework and Xcode v4.5.

Here’s some code:

ViewController.h:

@property (strong, nonatomic) IBOutlet UILabel *myLabel;

ViewController.m:

- (void)viewDidLoad {
    myLabel.text = @"test";
}

ViewControllerTests.m:

ViewController *vc = [[ViewController alloc] init];

// create view in memory by calling view's getter
[vc view];

// set myLabel.text to "test"
[vc viewDidLoad];

[vc.myLabel shouldNotBeNil];
[[vc.myLabel.text should] equal:@"test"];

The above test fails, telling me that myLabel is nil.

This StackOverflow answer states that calling loadView will create the view hierarchy in memory and thereby prepare any subviews for use. According to the documentation, simply calling a ViewController view’s getter will call loadView for you and create all views. Therefore, I find it extremely odd that myLabel = nil at the time of testing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:18:30+00:00Added an answer on June 13, 2026 at 1:18 am

    In your view controller, you are not assigning a value to self.myLabel.

    self.myLabel = [[UILabel alloc] init];
    

    This means self.myLabel has a nil value, so the following sequence occurs: [self myLabel] returns nil, [nil setText:@"test"] does nothing. All of this is standard behavior.

    When you load the view controller from the nib or storyboard, the label is assigned according to how the outlets are drawn in IB, but calling ViewController *vc = [[ViewController alloc] init] does not loading the nib or storyboard.


    Update

    After re-reading my post, I think I left the wrong impression.

    You need to load from a nib or storyboard, not add self.myLabel = [[UILabel alloc] init].

    To load from a nib, use -initWithNibName:bundle: instead of -init. To load from a storyboard, first load the storyboard use +[UIStoryboard storyboardWithName:bundle:], then load the view controller from the storyboard using -[UIStoryboard instantiateViewControllerWithIdentifier:].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing unit test for an existing code which is like this class
I'm writing a unit test and the piece of code its testing requires that
i am writing unit test for a C# project. One of the event handler
I have similar code for writing unit test where I need to check the
I am writing my unit test cases for a Java project using Scala (JUnit
I am writing unit test for zend project, I want to know <?php class
Busy writing a unit test for a controller that produces a view-model that includes
I'm writing a unit test with Boost.Unit, and I would like to include basic
I'm writing a unit test for a custom model binder (MVC 3) and want
I am writing a unit test and the controller method is throwing an exception

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.