EXC_BAD_ACCESS ao acessar parâmetros no andDo do OCMock
Eu estou tentando escrever um bloco de código usando o stub do OCMocke fazer método.
Neste caso, a classe de extensão UIImageView está sendo testada. Quero verificar se as chamadas de extensão[self setImage:] com parâmetro que não é nulo (mais tarde será usada outra comparação de imagem).
Ao usar o OCMocke fazer método, o teste falha com EXC_BAD_ACCESS depois que o bloco é concluído.
id mockView = [OCMockObject mockForClass:[UIImageView class]];
[[[mockView stub] andDo:^(NSInvocation *invocation)
{
UIImage *img;
[invocation getArgument:&img atIndex:2]; <---- line causing the exception
somebodySetImage |= (img != nil);
}] setImage:OCMOCK_ANY];
[mockView do_something_that_calls_setImage];
A única solução que encontrei agora é usarandCall ao invés dee fazer, mas isso dificulta o teste.
Posso evitar o acidente come fazer?
ATUALIZAR Bem, vou tentar dar um exemplo melhor aqui: aqui está a nova peça do código de teste:
- (void)testDownloadingThumbnail
{
PInfo *_sut = [[PInfo alloc] init];
__block id target = nil;
id mock = [OCMockObject mockForClass:[NSOperationQueue class]];
[[[mock expect] andDo:^(NSInvocation *inv)
{
NSInvocationOperation *op;
[inv getArgument:&op atIndex:2];
target = [[op invocation] target]; /* replacing this line with STAssert does not help either */
}] addOperation:OCMOCK_ANY];
[_sut setDownloadQueue:mock];
[_sut startDownloadingImagesAsync:YES];
[mock verify];
STAssertEqualObjects(target, _sut, @"invalid op target");
}
Aqui está o código testado (método único do PInfo):
- (void)startDownloadingImagesAsync:(bool)isThumbnailImg
{
NSInvocationOperation *inv;
inv = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(loadThumbnailWorker:)
object:nil];
[[self downloadQueue] addOperation:inv];
}
O código ainda falha ao sair dostartDownloadingImagesAsync com EXC_BAD_ACCESS. Se eu adicionar um ponto de interrupção dentro doe fazer bloco, vejo que o controle atinge esse ponto e recupera objetos corretos viagetArgument.
No entanto, se eu usargetArgument dentro do bloco, ele cai o que eu tento fazer.
P.S. Obrigado por ajuda.