具有外观的OCMock部分模拟对象

时间:2014-07-09 16:42:32

标签: objective-c unit-testing mocking ocmock

注意:我正在使用OCMock 3.0.2和Xcode 5.1.1。

我有一个对象作为它拥有的另一个对象的Facade。我构建它的方式是在我想要暴露的方法的自有对象中创建一个协议:

@protocol SyncEngineProtocol <NSObject>
- (void) updateAllGroups;

... more methods....

@end
@interface CoreDataSyncEngine : NSObject <SyncEngineProtocol>
@end

然后在Facade中,我创建了一个符合此协议的类别,但我没有实现该类别。相反,我将这些调用转发给拥有的对象:

@interface ModelManager (SyncMethods) <SyncEngineProtocol>
@end

@implementation ModelManager
...
- (id) forwardingTargetForSelector:(SEL)aSelector{
  if ([_syncEngine respondsToSelector:aSelector]){
    return _syncEngine;
  }
  return nil;
}
...
@end

我遇到两个问题,我不知道它们是否相关:

  1. 我遇到的主要问题是在拥有对象(ModelManager)上调用Protocol方法时,OCMock会抛出一个无法识别的选择器异常。
  2. 我遇到的第二个问题是每次运行测试时,当我尝试创建同步引擎的类模拟时,我会在OCMock框架中抛出几个异常,这会暂停代码执行。我可以继续通过他们,一切“看起来”正常工作,但显然有些不对劲。这是异常的堆栈跟踪:
  3. 0   0x00000001020c2973 in objc_exception_throw ()
    ...
    5   0x0000000102352ead in +[NSMethodSignature signatureWithObjCTypes:] ()
    6   0x000000010dc128ac in +[NSObject(OCMAdditions) instanceMethodForwarderForSelector:] at /tmp/ocmock.13557/Source/Source/OCMock/NSObject+OCMAdditions.m:29
    7   0x000000010dc11fbb in -[OCClassMockObject setupForwarderForClassMethodSelector:] at /tmp/ocmock.13557/Source/Source/OCMock/OCClassMockObject.m:110
    8   0x000000010dc1292e in +[NSObject(OCMAdditions) enumerateMethodsInClass:usingBlock:] at /tmp/ocmock.13557/Source/Source/OCMock/NSObject+OCMAdditions.m:48
    9   0x000000010dc11e63 in -[OCClassMockObject prepareClassForClassMethodMocking] at /tmp/ocmock.13557/Source/Source/OCMock/OCClassMockObject.m:97
    10  0x000000010dc11b5b in -[OCClassMockObject initWithClass:] at /tmp/ocmock.13557/Source/Source/OCMock/OCClassMockObject.m:31
    11  0x000000010dc1316f in +[OCMockObject mockForClass:] at /tmp/ocmock.13557/Source/Source/OCMock/OCMockObject.m:48
    12  0x000000010dc13231 in +[OCMockObject niceMockForClass:] at /tmp/ocmock.13557/Source/Source/OCMock/OCMockObject.m:64
    13  0x000000010dc06abb in +[CSGroupControllerTests setUp] at /Users/aaron/Dropbox/Client Projects/CrowdSporting/Codebase/crowdsportingTests/CSGroupControllerTests.m:56
    14  0x000000010dee148b in -[XCTestSuite performTest:] ()
    15  0x000000010dee2a75 in -[XCTest run] ()
    16  0x000000010dee14df in -[XCTestSuite performTest:] ()
    17  0x000000010dee2a75 in -[XCTest run] ()
    18  0x000000010dee14df in -[XCTestSuite performTest:] ()
    19  0x000000010dee2a75 in -[XCTest run] ()
    20  0x000000010dee41b4 in +[XCTestProbe runTests:] ()
    ...
    27  0x0000000100b52e33 in UIApplicationMain ()
    

    我不认为这两者是相关的,但由于我同时遇到问题,我不能忽视时机。

    任何帮助都将不胜感激。

    更新

    我降级到OCMock 2.2.4并且OCMock框架中抛出的所有异常都没有了,哪种情况很糟糕,因为我更喜欢3.0中的新语法。

    我仍然不能嘲笑门面。似乎OCMock要求实现所有方法......没有动态转发。当然,我可以这样做,但这很痛苦。

0 个答案:

没有答案