Code Monkey home page Code Monkey logo

Comments (7)

mbrandonw avatar mbrandonw commented on July 30, 2024 1

Ah I see, the errorWith static method returns an optional view controller. You could force unwrap the controller in your test and then it should work:

-assertSnapshot(matching: vc, as: .image)
+assertSnapshot(matching: vc!, as: .image)

Too bad the Swift error messaging was really bad.

from swift-snapshot-testing.

mbrandonw avatar mbrandonw commented on July 30, 2024

Most likely this means that the .image snapshot strategy does not work with the value you are trying to match against. In this case, is vc really a UIViewController subclass? Could you share some more code with GeneralAlert's definitions?

from swift-snapshot-testing.

yaakovgamliel avatar yaakovgamliel commented on July 30, 2024

This is the implementation of my controller

import UIKit
import Core

typealias GeneralAlert = GeneralAlertViewController

class GeneralAlertViewController: UIViewController {
    
    var cancelAction: (() -> Void)?
    var alertTitle: String? { didSet { titleLabel?.text = alertTitle } }
    var body: String? { didSet { bodyLabel?.text = body } }
    var buttonText: String? { didSet { OKButton?.setTitle(buttonText, for: .normal) } }

    @IBOutlet private weak var titleLabel:UILabel!
    @IBOutlet private weak var bodyLabel:UILabel!
    @IBOutlet private weak var OKButton:UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        titleLabel.text = alertTitle
        bodyLabel.text = body
        OKButton.setTitle(buttonText, for: .normal)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        subscribeTimeoutHandler()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        unsubscribeTimeoutHandler()
    }

    override func timeoutNotificationReceived() {
        dismiss(animated: true, completion: nil)
    }

    func present(with vc: UIViewController, animated: Bool = true, completion: (() -> Void)? = nil) {
        DispatchQueue.main.async {
            vc.present(self, animated: animated, completion: completion)
        }
    }

    static func errorWith(body:String?, buttonText: String = Messages.ok(), cancelAction:(() -> Void)? = nil) -> GeneralAlertViewController? {
        return with(title: Messages.defaultErrorTitle(), body: body, buttonText: buttonText, cancelAction: cancelAction)
    }

    static func with(title:String?, body:String?, buttonText: String = Messages.ok(), cancelAction:(() -> Void)? = nil) -> GeneralAlertViewController? {
        let identifier = String(describing: GeneralAlertViewController.self)
        let vc = UIStoryboard.generalAlert().instantiateViewController(withIdentifier: identifier)
        guard let alert = vc as? GeneralAlertViewController else {
            return nil
        }

        alert.alertTitle = title
        alert.body = body
        alert.cancelAction = cancelAction
        alert.buttonText = buttonText
        alert.modalPresentationStyle = .formSheet
        alert.modalTransitionStyle = .crossDissolve
        alert.preferredContentSize = CGSize(width: 600, height: 200)

        return alert
    }

    @IBAction func okButtonTouchUpInside(sender: UIButton) {
        guard let action = cancelAction else {
            dismiss(animated: true, completion: nil)
            return
        }

        dismiss(animated: true) { action() }
    }

    // MARK: - Helper functions

    @objc func forcedDismiss() {
        if self.view.window == nil { return }
        dismiss(animated: true, completion: nil)
    }
}

from swift-snapshot-testing.

yaakovgamliel avatar yaakovgamliel commented on July 30, 2024

Unwrapping the controller solved the issue :), thanks!!

from swift-snapshot-testing.

mbrandonw avatar mbrandonw commented on July 30, 2024

Great! Glad to hear. Will close out this issue.

from swift-snapshot-testing.

stephencelis avatar stephencelis commented on July 30, 2024

(Filed a bug report with Swift here: https://bugs.swift.org/browse/SR-9598)

from swift-snapshot-testing.

jeannustre avatar jeannustre commented on July 30, 2024

Getting the same error in 1.11.0 with a non-optional UIViewController.

Explicitly specifying the format works :

assertSnapshots(matching: vc, as: [Snapshotting<UIViewController, UIImage>.image])

Maybe a compiler cache issue since all my other tests compile fine using :

assertSnapshots(matching: vc, as: .image)

from swift-snapshot-testing.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.