iPhone SenTestingKit未能在UnitTestBundle目标中找到AppDelegate

iPhone SenTestingKit未能在UnitTestBundle目标中找到AppDelegate

问题描述:

我正尝试使用XCode 3.2.3为iPhone应用程序设置我的单元测试。iPhone SenTestingKit未能在UnitTestBundle目标中找到AppDelegate

我跟着http://developer.apple.com/iphone/library/documentation/xcode/conceptual/iphone_development/135-Unit_Testing_Applications/unit_testing_applications.html

尽可能接近我所能的步骤,但我在努力建立收到此错误信息: “(!(yourApplicationDelegate)=无)”应该是真实的。 UIApplication的未能找到AppDelegate中

(这是缺省的普通测试使用的应用单元测试为真)

我建立我的应用程序的测试目标,如文档中进行了讨论,并得到这个错误。

我是否需要确保所有非测试用例源都在我的测试包中?

混淆

-

我包含在源文件到我的项目,它编译罚款,但它仍然受到同样的单元测试错误:

"((yourApplicationDelegate) != nil)" should be true. UIApplication failed to find the AppDelegate

原代码与模板代码相同:

-(void) testAppDelegate { 

    id yourApplicationDelegate = [[UIApplication sharedApplication] delegate]; 
    STAssertNotNil(yourApplicationDelegate, @"UIApplication failed to find the AppDelegate"); 
} 
+1

是的,您的应用程序源应该包含在测试目标的编译源构建阶段。你能发布你的单元测试代码和实际错误吗? – 2010-07-08 19:28:08

+0

@chrispix - 侧面的问题,有没有一种方法可以自动将所有代码扫描到测试目标中,而无需每次都明确添加它们以创建新类? – groundhog 2010-07-09 02:28:48

+1

我不知道有什么方法可以“自动”将其扫描到测试目标中。但是,当您使用XCode创建新类时,对话框会询问您将其添加到哪些目标,并默认为您上次选择的任何目标。根据我的经验,我添加的每个类都被添加到我的测试目标中 - 它可以是我的应用中的测试类或类,它将以某种方式进行测试。所以我的测试目标总是在该对话框中检查。问题实际上是相反的 - 记住不要在你的应用程序目标中包含测试类。 – 2010-07-09 05:39:50

我不能直接回答你的问题,因为我从来没有得到应用程序测试来使用OCUnit。我了解许多人使用Google Toolbox for Mac iPhone单元测试库或GHUnit来简化此过程。如上面链接的Blazing Cloud帖子所指出的,OCUnit(和GTM/GHUnit)比Rspec风格更具JUnit风格。在Pivotal中,我们几乎所有的东西都使用了Rspec,我们希望Objective C的东西类似。因此,我们编写了Cedar,我们有意将其设计为与UIKit一起使用,在iOS设备上运行等等。它可以免费获得并且是开源的;如果您对此有任何疑问,可以发送电子邮件至讨论组:[email protected]

根据Apple的文档,有2种单元测试。

  • 逻辑测试
  • 应用测试

应用测试工作仅在硬件设备。如果你正在使用模拟器,它将不起作用。

看到这个: http://developer.apple.com/library/ios/#documentation/Xcode/Conceptual/iphone_development/135-Unit_Testing_Applications/unit_testing_applications.html

+2

Mate,这完全是从苹果文档复制粘贴的:_“因为应用程序测试**可以在模拟器和设备上运行**,您还可以使用这些测试来执行硬件测试,例如获取设备的位置您的应用程序“。因此,应用程序测试也可以在模拟器中运行,而不仅仅在设备上运行。 – 2011-11-15 12:54:17

这是OP,如果问题依然存在,仍然是相关的,并为论坛浏览器:

我也一样,随后在iOS开发的gudielines单元和应用程序测试指南。 我也一样,下面的应用程序测试指南时,有一个问题,因为每个:

Test Suite 'FirstSuite' started at 2010-12-08 21:30:33 GMT 0000 
Test Case '-[FirstSuite testAppDelegate]' started. 
/Users/TomCruise/Documents/iPhoneDevelopment/PickerLeak/FirstSuite.m:22: error:-[FirstSuite testAppDelegate] : "((app_delegate) != nil)" should be true. Cannot find the application delegate. 
Test Case '-[FirstSuite testAppDelegate]' failed (0.000 seconds). 

即应用程序委托未找到预期。

问题是,当我以为我遵循指导原则时,我没有。显然,应用程序测试仅在设备上运行。所以我从Simulator切换到Device(用于AppTesting目标),连接我的iPhone,然后执行Build + Run(cmd + return/enter)。应用程序自己安装,并在几秒钟后退出代码1(我猜这表示错误的返回代码)。

应用后退出了,我做了SHIFT + CMD + R,(UPARROW + CMD + R)获取该日志的提示,可以阅读以下内容:

2010-12-08 22:59:59.163 PickerLeak[5356:307] -[UIPickerView setFrame:]: invalid height value 200.0 pinned to 180.0 
Test Suite 'All tests' started at 2010-12-08 22:00:00 GMT 0000 
Test Suite '/var/mobile/Applications/{number}/PickerLeak.app/PickerLeakTests.octest(Tests)' started at 2010-12-08 22:00:00 GMT 0000 
Test Suite 'FirstSuite' started at 2010-12-08 22:00:00 GMT 0000 
**Test Case '-[FirstSuite testAppDelegate]' started. 
Test Case '-[FirstSuite testAppDelegate]' passed (0.001 seconds).** 
Test Case '-[FirstSuite testFail]' started. 
/Users/TomCruise/Documents/iPhoneDevelopment/PickerLeak/FirstSuite.m:17: error: -[FirstSuite testFail] : Must... fail. 
Test Case '-[FirstSuite testFail]' failed (0.003 seconds). 
Test Suite 'FirstSuite' finished at 2010-12-08 22:00:00 GMT 0000. 
Executed 2 tests, with 1 failure (0 unexpected) in 0.004 (0.008) seconds 

所以......万岁! :)

Godspeed,开发人员。

确保指定了单元测试包的BUNDLE_LOADER和TEST_HOST构建设置。

BUNDLE_LOADER = $(BUILT_PRODUCTS_DIR)/YourApp.app/YourApp 
TEST_HOST = $(BUNDLE_LOADER) 

此外,在Xcode4,许多的苹果的指南中所描述的步骤是不需要的(例如复制的应用程序的目标)。我建议创建新的项目,包括单元测试和环顾四周。

+0

在整个下午尝试了一切之后,终于做到了。非常感谢! – 2011-05-11 04:09:07

+0

谢谢!我还需要将我的主要目标添加为Unit Test bundle的“Target Dependency”(目标设置 - > Build Phases)。 – 2011-07-19 17:25:14

我已经在这个上掉了两次。 您需要设置BUNDLE_LOADER和TEST_HOST构建设置才能使其工作。 但这还不够,您还需要设置Bundle Loader和Test Host。 请检查how to implement application tests in xcode4?,该答案对我来说工作得很好。