0

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.

  • 1. That initializer is an abomination 2. since you're not initializing `viewTitle` anywhere I'm guessing it's supposed to be set up in IB. Are you sure the outlet is properly connected? – mag_zbc Jul 04 '18 at 08:46
  • Thank you for your help, it's my mistake that I selected the File's Owner option when I connected the outlet.It should be my custom view. I am a beginner of IOS so that I didn't do it well about the initializer, would you like to tell me how to improve my initializer?Thank you very much. – Dave Luo Jul 05 '18 at 02:28

0 Answers0