【问题标题】:OCMock partial mock gives unexpected callOCMock 部分模拟给出了意外的调用
【发布时间】:2013-06-03 14:06:52
【问题描述】:

我有一个测试可以验证(使用 OCMock)在发送某个通知时是否调用了某个方法:

- (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];
}

这是错误:

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

这里是 viewDidLoad 方法(有点乱):

- (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];
        }
    }
}

【问题讨论】:

    标签: ios ocunit ocmock


    【解决方案1】:

    我的猜测是,无论您在哪里声明 vc.view 为滚动视图,都不会被执行。如果你 stub [IssueDetailsViewController view] 返回一个 UIScrollView 类型的 Mock Object 怎么办?

    【讨论】:

    • 非常感谢您,先生!你是救世主。有时我觉得做 TDD/UnitTesting 的 iOS 社区太小了,我找不到任何解决我的问题的方法。也许我错了。无论如何,我的代码需要重构,因为我的视图是滚动视图的东西有点像 hack。我重新连接了 xib,以便视图控制器的视图是滚动视图。你知道如何解决这个问题吗?
    • 这可能会浪费视图,但我可能只是让滚动视图成为主视图的子视图。很高兴能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多