使用OCMock 1.77进行iOS4和Xcode 4/SDK4.3的单元和应用程序测试

Eri*_*rik 7 iphone xcode objective-c ocmock

我正在尝试使用OCMock 1.77进行iOS4和Xcode 4/SDK4.3的单元和应用程序测试.我按照说明使用OCMock作为静态库,在这里找到:http://www.mulle-kybernetik.com/software/OCMock/.没有OCMock,单元和应用程序测试运行良好.

当我添加OCMock并尝试运行OCMock的模拟器测试套件(单元测​​试)时,我的测试装置崩溃了代码134.测试装置运行良好的设备(应用程序测试).如果我查看控制台,我会看到下面的消息 - 这表明我没有按照上述URL中的说明添加-force_load链接器标志.但我有......有什么想法吗?

我看了一下:测试装置在iOS 4上使用OCMock验证代码134异常退出,这表明这种行为是一个错误 - 但我不确定它是否与我运行OCMock测试套件的情况相同.如果它是一个bug,有没有办法在单元测试中使用模拟?

TIA.

=====

控制台输出:

3/30/11 1:02:32 AM  otest[38552]    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '** Expected method not present; the method getArgumentAtIndexAsObject: is not implemented by NSInvocation. If you see this exception it is likely that you are using the static library version of OCMock and your project is not configured correctly to load categories from static libraries. Did you forget to add the -force_load linker flag?'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x004a05a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x002cf313 objc_exception_throw + 44
    2   CoreFoundation                      0x00458ef8 +[NSException raise:format:arguments:] + 136
    3   CoreFoundation                      0x00458e6a +[NSException raise:format:] + 58
    4   LogicTests                          0x00f89de4 +[OCMockObject initialize] + 115
    5   libobjc.A.dylib                     0x002cfd9b _class_initialize + 380
    6   libobjc.A.dylib                     0x002d773f prepareForMethodLookup + 73
    7   libobjc.A.dylib                     0x002ce069 lookUpMethod + 86
    8   libobjc.A.dylib                     0x002ce1d6 _class_lookupMethodAndLoadCache + 40
    9   libobjc.A.dylib                     0x002e10e3 objc_msgSend + 87
    10  SenTestingKit                       0x20108824 +[NSObject(SenTestRuntimeUtilities) senAllSubclasses] + 107
    11  SenTestingKit                       0x201074a7 +[SenTestSuite updateCache] + 39
    12  SenTestingKit                       0x20107443 +[SenTestSuite suiteForBundleCache] + 92
    13  SenTestingKit                       0x201073a4 +[SenTestSuite testSuiteForBundlePath:] + 108
    14  SenTestingKit                       0x2010606b +[SenTestProbe specifiedTestSuite] + 332
    15  SenTestingKit                       0x20106792 +[SenTestProbe runTests:] + 156
    16  otest                               0x000023c7 0x0 + 9159
    17  otest                               0x000025f2 0x0 + 9714
    18  otest                               0x0000209a 0x0 + 8346
    19  otest                               0x00002049 0x0 + 8265
)
Run Code Online (Sandbox Code Playgroud)

Enr*_*ani 8

  • http://ocmock.org/#download下载OCMock*.**(撰写本文时1.77)
  • 在OCMock /(或其他文件夹)中解压缩 - >您将有两个文件夹"Release"和"Source"
  • 复制xCode项目文件夹中的"Release/Library"文件夹
  • 使用xcode链接文件,拖动项目中的所有"Library"文件夹
    • 选择应用程序TEST目标作为目标!
  • 转到测试目标的"构建设置"
    • 在"搜索路径"下添加:+"标题搜索路径" - >插入OCMock标题的路径(.h)(类似$(PROJECT_DIR)/ Library/Headers)+"Library Search Paths" - >插入你的OCMock库的路径(.a)(类似于$(PROJECT_DIR)/ Library /)注意:作为路径,你也可以使用$(PROJECT_DIR)或$(SRCROOT)/并选择递归复选框以避免插入完整路径
      • 在"链接"下添加:+"其他链接标志" - > -all_load

一切都应该正常工作.要验证:

#import <OCMock/OCMock.h>
// simple test to ensure building, linking, 
// and running test case works in the project
- (void)testOCMockPass {
id mock = [OCMockObject mockForClass:NSString.class];
[[[mock stub] andReturn:@"mocktest"] lowercaseString];
NSString *returnValue = [mock lowercaseString];
STAssertEqualObjects(@"mocktest", returnValue, @"Should have returned the expected string.");
}

- (void)testOCMockFail {
id mock = [OCMockObject mockForClass:NSString.class];
[[[mock stub] andReturn:@"mocktest"] lowercaseString];
NSString *returnValue = [mock lowercaseString];
STAssertEqualObjects(@"thisIsTheWrongValueToCheck", returnValue, @"Should have returned the expected string."); 
}
Run Code Online (Sandbox Code Playgroud)

非常感谢:

恩里科博塔尼


Chr*_*lay 3

我可以通过删除 -force_load 标志来重现这一点。检查以确保它设置在您的测试目标上,而不是您的主目标上,并且它指向静态库的正确位置。就我而言,那就是

-force_load $(PROJECT_DIR)/Libraries/libOCMock.a
Run Code Online (Sandbox Code Playgroud)

您链接的另一个SO 问题只是说,当模拟的verify方法失败时,它会从 NSInitation 上的类别方法引发异常,这会导致 otest 崩溃。它仍然有效;该消息比断言失败更加神秘,XCode 在测试类中将其标记为错误。