1

I have a test that verifies (with OCMock) that a method gets called when a certain notification is sent:

- (void)testThatVCRegistersToLocationUpdateNotification {
    IssueDetailsViewController* vc = [[IssueDetailsViewController alloc] init];
    id mockVC = [OCMockObject partialMockForObject:vc];
    [[mockVC expect] locationDidUpdate:[OCMArg any]];
    [vc viewDidLoad];

    [[NSNotificationCenter defaultCenter] postNotificationName:LOCATION_DID_UPDATE object:nil];

    [mockVC verify];
}

This is the error:

error: testThatVCRegistersToLocationUpdateNotification (IssueDetailsViewControllerTests) failed: -[UIView setContentSize:]: unrecognized selector sent to instance 0x82cfb30

Here is the viewDidLoad method (it's kinda cluttered):

- (void)viewDidLoad
{
    [super viewDidLoad];

    //[self.contentView setBackgroundColor:[UIColor redColor]];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationDidUpdate:) name:LOCATION_DID_UPDATE object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidSelectCategories:) name:USER_DID_SELECT_CATEGORIES object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

    UIView* keyboardAccessoryView = [STUtils getKeyboardAccessoryView];
    [self.titleTextField setInputAccessoryView:keyboardAccessoryView];
    [self.descriptionTextView setInputAccessoryView:keyboardAccessoryView];
    [self.addressTextField setInputAccessoryView:keyboardAccessoryView];

    [self.view addSubview:self.contentView];
    ((UIScrollView *)self.view).contentSize = self.contentView.frame.size;
    [((UIScrollView*)self.view) setShowsVerticalScrollIndicator:NO];

    [self.descriptionTextView.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.descriptionTextView.layer setBorderWidth:1.0f];

    //[self.categoriesTextField setUserInteractionEnabled:YES];


    UIBarButtonItem* reportButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(reportButtonClicked:)];

    self.navigationItem.rightBarButtonItem = reportButton;

    if (ISSUE_CREATION_MODE == self.viewControllerMode) {
        [[STLocationManager sharedInstance] startLocating];
    }

    if (ISSUE_EDITING_MODE == self.viewControllerMode) {

        self.titleTextField.text = self.parameters[@"title"];
        self.longitudeLabel.text = [NSString stringWithFormat:@"%@", self.parameters[@"lon"]];
        self.latitudeLabel.text  = [NSString stringWithFormat:@"%@", self.parameters[@"lat"]];

        if (self.parameters[@"description"]) {
            self.descriptionTextView.text = self.parameters[@"description"];
        }

        if (self.parameters[@"categories"]) {
            self.categories = self.parameters[@"categories"];
            self.categoriesTextField.text = [self.categories componentsJoinedByString:@", "];
        }

        if (self.parameters[@"address"]) {
            self.addressTextField.text = self.parameters[@"address"];
        }

        for (int imageIndex=0;imageIndex<self.images.count;imageIndex++) {

            UIImageView *imageView = [[UIImageView alloc] init];
            [imageView setTag:PHOTO_TYPE_EXISTING];
            [imageView setImageWithURL:[NSURL URLWithString:[WebserviceAPI urlByAppendingString:self.images[imageIndex]]]];
            [imageView setContentMode:UIViewContentModeScaleAspectFill];
            [self addImageViewToCarousel:imageView];
        }
    }
}
Ilea Cristian
  • 5,741
  • 1
  • 23
  • 37

1 Answers1

1

My guess would be that wherever you are declaring vc.view to be a scrollview is not being executed. What if you stub [IssueDetailsViewController view] to return a Mock Object of type UIScrollView?

Ben Flynn
  • 18,524
  • 20
  • 97
  • 142
  • Thank you very much, sir! You're a saviour. Sometimes I feel like the iOS community that does TDD/UnitTesting is so small that I won't find any solutions for my problems. Maybe I'm wrong. Anyway, my code needs refactoring because the thing with my view being a scrollview is kind-of-a-hack. I rewired the xib so that the view of the view-controller is a scrollview. Do you have any ideea how to fix this? – Ilea Cristian Jun 03 '13 at 20:01
  • 1
    It might be a waste of a view, but I'd probably just make the scrollview the child of the main view. Glad to be of help! – Ben Flynn Jun 03 '13 at 20:57