OCMock是一个NSOperation [英] OCMock an NSOperation
问题描述
我正在尝试编写一些单元测试来测试我们正在编写的一些自定义 NSOperations
。我想做的是创建一个 NSOperation
的模拟并将其放在 NSOperationQueue
上,然后等待为了它完成。我知道我可以调整方法,而不是使用 OCMoc
k,但我真的不想这样做。我想使用 OCMock
。我试图运行的代码如下所示:
I am trying to write some Unit Tests to test some custom NSOperations
that we are writing. What I'd like to do is create a Mock of the NSOperation
and put it on the NSOperationQueue
, and then wait for it to finish. I know I can swizzle the methods and not use OCMoc
k at all, but I really don't want to do that. I'd like to use OCMock
. The code I'm trying to run is something like the following:
MYOperation *operation = [MYOperation new];
id mockOperation = [OCMockObject partialMockForObject:operation];
[NSOperationQueue *queue = [NSOperationQueue new];
[queue setMaxConcurrentOperationCount:1];
[queue addOperation:mockOperation];
当单元测试到达此行时:
When the unit test gets to this line:
[queue addOperation:mockOperation];
我接到一个解除分配对象异常的调用。任何人对我如何克服这个问题都有任何建议吗?
I get a call to a deallocated object exception. Anyone have any suggestions on how I can overcome this?
推荐答案
如果你正在使用ARC,操作
可能在您创建模拟后立即发布,因为它不会再次访问。如果你把它改成这个,它应该修正错误:
If you're using ARC, operation
is probably released right after you create the mock, as it's not accessed again. If you change it to this, it should fix the error:
[queue addOperation:operation];
...无论如何你应该做的 - 你正在测试你的对象,而不是模拟。
...which you should be doing anyways--you're testing your object, not the mock.
这篇关于OCMock是一个NSOperation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!