5
func testExample() {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    // some code ...
}

The above function always crash and gives this error:

Could not cast value of type 'MyAppName.AppDelegate' (0x10dc09e80) to 'MyAppNameTests.AppDelegate' (0x11cc190c0).

Mohammed
  • 1,432
  • 4
  • 18
  • 34

1 Answers1

3

You are probably compiling AppDelegate into your test target. Don't do this.

Instead, only compile into your normal app target MyAppName. In you test class write in XCode 7

@testable import MyAppName

and before XCode 7

import MyAppName
Gerd Castan
  • 6,275
  • 3
  • 44
  • 89
  • writing "@testable import MyAppName" in my test class gives me this error: Unknown attribute 'testable' – Mohammed Aug 25 '15 at 14:16
  • Then you don't use XCode 7. Before XCode 7 remove '@testable'. – Gerd Castan Aug 25 '15 at 14:24
  • that did not work, too – Mohammed Aug 25 '15 at 15:15
  • Same error message? Are sure that the target of AppDelegate is not MyAppNameTest? – Gerd Castan Aug 25 '15 at 17:59
  • @GerdCastan can you elaborate on your answer a bit? Why is compiling into your test target bad? How can I "only compile into your normal app target". I have a similar issue, but when I 'import MyAppName' I get 'No such module "MyAppName"', which I suspect is about something I don't understand, but *you do*... – meriial Aug 30 '15 at 02:57
  • Replace MyAppName in the import statement exactly with the name of the target you chose for your App. I hope you have chosen to have no space in that target name. – Gerd Castan Aug 30 '15 at 06:32