2017-08-25 16 views
6

मैं एक लेबल (टोस्ट लेबल) की उपस्थिति का परीक्षण करने के लिए संघर्ष कर रहा हूं, जो कि मेरे पास गलत ईमेल में प्रवेश करते समय संक्षेप में एनिमेट करता है।दिखाई देने वाले एनिमेटेड लेबल का परीक्षण पाठ

private func registerNewUser(email: String, password: String, confirmationPassword: String) { 
    if password == confirmationPassword { 
     firebaseData.createUser(email: email, password: password, completion: { (error, _) in 
      if let error = error { 
       self.showToast(in: self.view, with: error.localizedDescription) 
      } else { 
       self.showToast(in: self.view, with: "Registered succesfully") 
       self.signInUser(email: email, password: password) 
      } 
     }) 
    } else { 
     //raise password mismatch error 
     print("password mismatch error") 
    } 
} 

func showToast(in toastSuperView: UIView, with text: String) { 
    let toastLabel = ToastLabel() 
    toastLabel.text = text 
    toastSuperView.addSubview(toastLabel) 
    layoutToastLabel(toastLabel) 
    animateToastLabel(toastLabel) 
} 

private func layoutToastLabel(_ toastLabel: ToastLabel) { 
    toastLabel.centerYToSuperview() 
    toastLabel.pinToSuperview(edges: [.left, .right]) 
} 

private func animateToastLabel(_ toastLabel: ToastLabel) { 
    UIView.animate(withDuration: 2.5, delay: 0, options: .curveEaseOut, animations: { 
     toastLabel.alpha = 0.0 
    }, completion: { _ in 
     toastLabel.removeFromSuperview() 
    }) 
} 

मैं सिर्फ इतना है कि त्रुटि पाठ वापस प्राप्त उपयोगकर्ता के बाद एक ईमेल कि पहले से ही लिया गया है में प्रवेश करती है firebase प्रकट होता है से परीक्षण करना चाहते हैं।

func testRegisteringWithUsedEmailDisplaysFirebaseError() { 
    let email = registeredEmail 
    let password = "password" 

    welcomeScreenHelper.register(email: email, 
           password: password, 
           confirmationPassword: password, 
           completion: { 

     let firebaseErrorMessage = "The email address is already in use by another account." 
     XCTAssert(self.app.staticTexts[firebaseErrorMessage].exists) 
    }) 
} 

func register(email: String, password: String, confirmationPassword: String, completion: (() -> Void)? = nil) { 
    let emailTextField = app.textFields[AccesID.emailTextField] 
    let passwordTextField = app.secureTextFields[AccesID.passwordTextField] 
    let confirmPasswordTextField = app.secureTextFields[AccesID.confirmPasswordTextField] 
    let registerButton = app.buttons[AccesID.registerButton] 

    emailTextField.tap() 
    emailTextField.typeText(email) 
    passwordTextField.tap() 
    passwordTextField.typeText(password) 
    registerButton.tap() 
    confirmPasswordTextField.tap() 
    confirmPasswordTextField.typeText(confirmationPassword) 
    registerButton.tap() 

    completion?() 
} 

जब मैं उम्मीदों और एक्ससीटीवाइटर जैसे अन्य टूल्स का उपयोग करता हूं तो परीक्षण और लेबल निश्चित रूप से दिखाई देने के बावजूद परीक्षण पास नहीं होते हैं। मुझे इस तरह का परीक्षण कभी नहीं करना पड़ा है, इसलिए मुझे यकीन नहीं है कि मैं गलत कहां जा रहा हूं, भले ही मुझे एनिमेटेड व्यू या कुछ परीक्षण करने के लिए कुछ अलग करना है।

Update1:

इसलिए मैं थोड़ा और खेलने जब मैं registerButton नल टोस्ट एकदम सही ढंग से लेकिन जब तक यह फिर से गायब हो गया है परीक्षण के लिए जारी नहीं करता प्रतीत होता है कि के बाद देख सकते हैं। मुझे यह अजीब लगता है क्योंकि यह रजिस्टर से सख्ती से जुड़ा हुआ नहीं है बटन अपने स्वयं के विचार हैं।

Update2:

मैं इस प्रकार अपने परीक्षण अद्यतन है:

func testRegisteringWithUsedEmailDisplaysFirebaseError() { 

    welcomeScreenHelper.register(email: registeredEmail, 
           password: password, 
           confirmationPassword: password, 
           completion: { 

     let firebaseErrorMessage = "The email address is already in use by another account." 

     let text = self.app.staticTexts[firebaseErrorMessage] 
     let exists = NSPredicate(format: "exists == true") 

     self.expectation(for: exists, evaluatedWith: text, handler: nil) 
     self.waitForExpectations(timeout: 10, handler: nil) 
     XCTAssert(self.app.staticTexts[firebaseErrorMessage].exists) 
    }) 
} 
के योग के साथ

:

override func setUp() { 
    app.launch() 
    UIView.setAnimationsEnabled(false) 
    super.setUp() 
} 

override func tearDown() { 
    if let email = createdUserEmail { 
     firebaseHelper.removeUser(with: email) 
    } 
    UIView.setAnimationsEnabled(true) 
    super.tearDown() 
} 

लेकिन अभी तक कोई भाग्य। मैं अभी भी देख सकता हूं कि func register में पंजीकरण बटन को टोस्ट शो टैप करने के बाद और अगली पंक्ति तब तक नहीं बुलाई जाती जब तक टोस्ट लेबल ने एनिमेटिंग समाप्त नहीं की हो।

+0

यह मुश्किल है, हालांकि अपने कोड में समस्या दिखाई देती है के लिए - वहाँ कुछ तरीके हैं लेकिन संदर्भ गुम है पसंद: 'func testRegisteringWithUsedEmailDisplaysFirebaseError()' निश्चित रूप से 'क्लास माईटेस्ट: XCTestCase' में है; 'func रजिस्टर है (ईमेल: स्ट्रिंग, पासवर्ड: स्ट्रिंग, पुष्टिकरण पासवर्ड: स्ट्रिंग, समापन: (() -> शून्य)? = शून्य) '' MyTest' 'का एक तरीका भी है? एक स्टब विधि की तरह लगता है। क्या आप किसी भी तरह 'फायरबेस डेटा' इंजेक्शन कर रहे हैं? अधिक संदर्भ देने से निश्चित रूप से प्रश्न की गुणवत्ता में सुधार होगा। –

+0

कृपया, –

उत्तर

3

वहाँ कई चीजें आप इस तरह में हल करने की जरूरत है परीक्षण:

  1. तो कोड परीक्षण किए जाने DispatchQueue.async उपयोग कर रहा है आप का उपयोग करना चाहिए XCTestCase.expectation
  2. यदि आपके द्वारा परीक्षण किए जाने वाले कोड में UIView.animate है (मुझे लगता है कि आपके उदाहरण में कोई है) परीक्षण से पहले UIView.setAnimationsEnabled(false) करें और परीक्षण समाप्त होने के बाद इसे वापस सक्षम करें ताकि उम्मीद पूरी होने के लिए एनीमेशन की प्रतीक्षा न करे। आप इसे XCTestCase.setUp और XCTestCase.tearDown विधियों में कर सकते हैं।
  3. यदि आपके द्वारा परीक्षण किए जा रहे कोड में एसिंक कॉल कर रहे सेवाओं जैसी निर्भरताएं हैं (मुझे लगता है कि firebaseData है) तो आपको या तो अपने मैक्स/स्टब्स इंजेक्ट करना चाहिए जो सिंक्रनाइज़ेशन से व्यवहार करें या XCTestCase.expectation का उपयोग करें और परीक्षण के दौरान एपीआई/नेटवर्क के लिए प्रार्थना करें दौड़ में हैं।

तो XCTestCase.expectation + UIView.setAnimationsEnabled(false) का उपयोग करना आपके लिए काम करना चाहिए। उच्च 0 घंटे के साथ बस XCTestCase.expectation भी काम करना चाहिए।

संपादित करें 1:

func test() { 
    let exp = expectation(description: "completion called") 
    someAsyncMethodWithCompletion() { 
     exp.fulfill() 
    } 
    waitForExpectations(timeout: 1) { _ in } 
    // assert here 
} 

तो अपने परीक्षा पद्धति होनी चाहिए: उम्मीद उपयोग करने के लिए सही रास्ता

func testRegisteringWithUsedEmailDisplaysFirebaseError() { 
    let exp = expectation(description: "completion called") 
    welcomeScreenHelper.register(email: registeredEmail, 
           password: password, 
           confirmationPassword: password, 
           completion: { exp.fulfill() }) 
    self.waitForExpectations(timeout: 10, handler: nil) 
    let firebaseErrorMessage = "The email address is already in use by another account." 
    XCTAssert(self.app.staticTexts[firebaseErrorMessage].exists) 
} 
+1

हाय, उत्तर देने के लिए धन्यवाद, कोड फिलहाल DispatchQueue का उपयोग नहीं करता है, लेकिन यह टोस्ट के लिए UIView.animate का उपयोग करता है। मैंने जैसा किया है जैसा आपने उल्लेख किया है क्योंकि आप प्रश्न में मेरे अपडेट 2 में देख सकते हैं लेकिन एक ही असफल परिणाम प्राप्त करें। आपकी दूसरी टिप्पणी के संबंध में मैं कुछ भी मजाक नहीं कर रहा हूं या कुछ भी नहीं दबा रहा हूं, 'रजिस्टर' विधि सिर्फ एक सहायक वर्ग के भीतर निहित है और फायरबेस को कहीं भी इंजेक्शन नहीं दिया गया है, लेकिन अगर उपयोगकर्ता ने प्रवेश किया है तो इसमें कोई त्रुटि नहीं है। त्रुटियों को एक टोस्ट में प्रस्तुत किया जाता है जो मैं परीक्षण करने की कोशिश कर रहा हूं। धन्यवाद – Wazza

-1
बजाय एक लेबल का उपयोग कर आप UIAlertController डिफ़ॉल्ट सिर्फ इस

func toast(message: String!) -> Void { 

     let controller: UIAlertController = UIAlertController(title: nil, message: message, preferredStyle: .alert) 
     self.present(controller, animated: true, completion: nil) 
     let when = DispatchTime.now() + 2.5 // change 2.5 to desired number of seconds 
     DispatchQueue.main.asyncAfter(deadline: when) { 
      // Your code with delay 
      self.dismiss(animated: true, completion: nil) 
     } 

    } 

की तरह कोशिश कर सकते हैं यह आईओएस में टोस्ट को दिखाने के लिए आसान तरीका है की

+0

संपादित करने के लिए धन्यवाद, जवाब देने के लिए धन्यवाद, लेकिन मुझे इसे कस्टम व्यू देखने की आवश्यकता है। इसके अलावा मेरा सवाल परीक्षा उत्तीर्ण होने के बारे में है। – Wazza

संबंधित मुद्दे