Dave Luo
Dave Luo

Reputation: 1

Why did get a nil when I test my custom view's subview in IOS(Objective-C)?

I want to test a custom view and when I test the subview of my custom view,I get a nil. This is the init code of my custom view

-(instancetype)init {
self = [super init];
if(self){
    self = [[[NSBundle mainBundle] loadNibNamed:@"ExistAccountView" owner:self options:nil] lastObject];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.translatesAutoresizingMaskIntoConstraints = YES;
    self.frame = CGRectMake((mScreenWidth - mExistAccountViewWitdh) / 2 + mExistAccountViewWitdh, ((mScreenHeight - mExistAccountViewHeight) / 2) - mScreenHeight * 0.11, mExistAccountViewWitdh,  mExistAccountViewHeight);
    [window addSubview:self.bgView];
    [window addSubview:self];
}
return self;

}

And here is my test code

fdescribe(@"ExistAccountView", ^{
__block ExistAccountView *subject;
__block ExistAccountView *view;

describe(@"Its dependencies", ^{
    beforeEach(^{
        subject = [[ExistAccountView alloc]init];
    });

    describe(@"load ExistAccountView", ^{
        it(@"should subject not be nil", ^{
            subject should_not be_nil;
        });

        it(@"should text of subject view be correct", ^{
            view.viewTitle should_not be_nil;
            //subject.viewTitle.text should equal(@"You Have an Account");
        });
    });

}

The viewTitle is a outlet UILabel of ExistAccountView(my test object),when I load the view , I want to test the text of this label, but it returned a nil, so what should do to test this label. It blocked me for a long time, I will feel happy if anyone can give me any help.Thank you.

Upvotes: 0

Views: 56

Answers (0)

Related Questions