5

I'm doing Unit Tests in Swift, and am running into a problem. Every time I run the tests, I get the error:

Could not cast value of type 'app.AppDelegate' to 'app_Tests.AppDelegate' 

I made sure that the AppDelegate was unchecked from the tests bundle, and the view controllers cannot access the app delegate anymore. Does anyone have a solution to this problem. Thanks in advance!

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
  • hi, is it what you expected, http://stackoverflow.com/questions/31990724/cannot-access-appdelegate-in-xctestcase-objects-or-anything-that-references-it keep me informed – Oleg G. Jun 28 '16 at 16:24
  • I think you will be looking something similar to the answer below http://stackoverflow.com/a/32161983/2010154 – NSAnant Jun 29 '16 at 15:33

1 Answers1

5

I think your view controllers has checked target membership in your test bundle. In this case you view controllers uses your tests module and App Delegate your real module.

Target Membership

If you have in your test file @testable import <module name> in the top, where your app name, for example app. Then you should have ability to your view controllers without checked target membership for your tests.

This thing comes with Swift 2.0 that was released in September 2015.

Alexander Zimin
  • 1,700
  • 1
  • 15
  • 19