-2

I am working in an ios app in Xcode 7.3.1.In that I am trying to start unit testing on my swift iOS app. I cannot see to access anything that uses my appDelegate.I tried the following to solve from the following.But still am getting the same issue.Please help me to solve this. link1, link2

I am getting the following issue,while i am running unit test. My sample code is

import UIKit
import XCTest

@testable import MyAppName

class FreshBossTests: XCTestCase {

    var login:LoginPageController!
    override func setUp() {
        super.setUp()
        login = LoginPageController()
    }
    override func tearDown() {
        super.tearDown()
    }
    func testlogin() {
        let email:String! = "xxx@gmail.com"
        if login.isValidEmail(email) == true {
            XCTAssertEqual(true, true,"email in valid  format")
        }
    }

In my LoginPageController,I am having the following code.

let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

In that line only am getting the error like

Could not cast value of type 'MyAppName.AppDelegate' (0x10dc09e80) to 'MyAppNameTests.AppDelegate' (0x11cc190c0).
Community
  • 1
  • 1
Madhumitha
  • 3,794
  • 8
  • 30
  • 45

3 Answers3

2

You can import the production module at the top of the XCTest file.

@testable import MyAppName

This will then let you cast like:

Swift 3

let appDelegate = UIApplication.shared.delegate as! MyAppName.AppDelegate

Justyn
  • 1,442
  • 12
  • 27
0

As matt suggest, use it like ProductModuleName.AppDelegate. So in your case it would be

let appDelegate = UIApplication.sharedApplication().delegate as! MyAppName.AppDelegate

MyAppName should be the name of your app target module, not your test target. If you are unsure about the module name search Product Module Name in app target build settings

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
0

In your setup() function instead of "login = LoginPageController()" use these lines

let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
    login = storyBoard.instantiateViewControllerWithIdentifier("LoginView") as! LoginPageController

I faced the same issue and this helped me out.

Gupta
  • 26
  • 5