EXC_BAD_ACCESS when accessing parameters in andDo of OCMock

前端 未结 3 1058
终归单人心
终归单人心 2021-02-07 11:37

I am trying to write an block of code using OCMock\'s stub andDo method.

In this case UIImageView extension class is being tested. I want to check that the exte

3条回答
  •  死守一世寂寞
    2021-02-07 12:00

    I think the problem is that you're trying to invoke a mock object directly. For what you're trying to do, you shouldn't need a mock object. Just call the method and verify that the image was set:

    expect(myObject.imageView.image).to.beNil();
    [myObject do_something_that_calls_setImage];
    expect(myObject.imageView.image).not.to.beNil();
    

    If you really want to use a mock for some reason, you could do it with a real UIImageView and a partial mock:

    UIImageView *imageView = myObject.imageView;
    id mockView = [OCMockObject partialMockForObject:imageView];
    __block BOOL imageSet = NO;
    [[[mockView stub] andDo:^(NSInvocation *invocation) {
          UIImage *img;
          [invocation getArgument:&img atIndex:2];
          imageSet = (img != nil);
      }] setImage:OCMOCK_ANY];
    
    [myObject do_something_that_calls_setImage];
    expect(imageSet).to.beTruthy();
    

提交回复
热议问题