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

发布于 2024-10-27 21:00:48 字数 2940 浏览 5 评论 0原文

我正在尝试使用 OCMock 1.77 通过 iOS4 和 Xcode 4/SDK4.3 进行单元和应用程序测试。我已按照此处的说明使用 OCMock 作为静态库: http://www .mulle-kybernetik.com/software/OCMock/。单元和应用程序测试在没有 OCMock 的情况下运行良好。

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

我看了这个: 测试rig 在 iOS 4 上使用 OCMock 验证时异常退出,代码为 134,这表明此行为是一个错误 - 但我不确定情况是否相同,因为我正在运行 OCMock 测试套件。如果这是一个错误,是否有在单元测试中使用模拟的解决方法?

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
)

I am trying to use OCMock 1.77 for unit and application testing with iOS4 and Xcode 4/SDK4.3. I have followed the instructions to do using OCMock as a static library found here: http://www.mulle-kybernetik.com/software/OCMock/. The unit and application tests run fine without OCMock.

When I add OCMock and try to run OCMock's test suite for the simulator (unit tests), my test rig crashes with code 134. The test rig runs fine for a device (application tests). If I look in the console, I see the message below -- which suggests that I haven't added the -force_load linker flag per the instructions at the above URL. But I have... Any thoughts?

I looked at this: Test rig exited abnormally with code 134 with OCMock verify on iOS 4 which suggests this behavior is a bug -- but I'm not sure it is the same situation since I am running the OCMock test suite. If it is a bug, is there a work-around to use mocks in unit testing?

TIA.

=====

Console output:

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
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兲鉂ぱ嘚淚 2024-11-03 21:00:48
  • http://ocmock.org/#download 下载 OCMock *.** (目前编写 1.77)
  • 在 OCMock/(或其他文件夹)中解压 -->您将有两个文件夹“Release”和“Release”。 “Source”
  • 复制 xCode 项目文件夹中的“Release/Library”文件夹
  • 将文件与 xcode 链接,拖动项目中的所有“Library”文件夹
    • 选择应用程序测试目标作为目标!
  • 转到测试目标的“构建设置”
    • 在“搜索路径”下添加:+“标题搜索路径”-->插入 OCMock headers(.h) 的路径(类似于 $(PROJECT_DIR)/Library/Headers)
      +“库搜索路径”-->插入 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."); 
}

非常感谢:

恩里科·博塔尼 (Enrico Bottani)

  • Download OCMock *.** from http://ocmock.org/#download (At the moment of this writing 1.77)
  • unpack in a OCMock/ (or another folder) --> You will have two folder "Release" & "Source"
  • Copy the "Release/Library" folder inside your xCode project folder
  • Link your file with xcode dragging all the "Library" folder inside your porject
    • Select as the target the application TEST target!
  • Go to the "build settings" of your test target
    • Under "Search Paths" add: +"Header Search Paths" --> insert the path to your OCMock headers(.h) (something like $(PROJECT_DIR)/Library/Headers)
      +"Library Search Paths" --> insert the path to your OCMock library(.a) (something like $(PROJECT_DIR)/Library/)
      Note: as path you can also use $(PROJECT_DIR) or $(SRCROOT)/ and select the recursive checkbox to avoid insert the ocmplete path

      • Under "Linking" add: + "Other Link Flags" --> -all_load

Everything should now work correctly. To verifiy:

#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."); 
}

Many thanks to:

Enrico Bottani

撩发小公举 2024-11-03 21:00:48

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

-force_load $(PROJECT_DIR)/Libraries/libOCMock.a

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

I can reproduce this by removing the -force_load flag. Check to make sure it's set on your test target, not your main target, and that it points to the correct location of the static library. In my case, that's

-force_load $(PROJECT_DIR)/Libraries/libOCMock.a

The other SO question you linked is just saying that when a mock's verify method fails, it throws an exception from a category method on NSInvocation, which causes otest to crash. It still works; the message is just more cryptic than an assertion failure, which XCode labels as an error in your test class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文