Comments (7)
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.
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.
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.
Unwrapping the controller solved the issue :), thanks!!
from swift-snapshot-testing.
Great! Glad to hear. Will close out this issue.
from swift-snapshot-testing.
(Filed a bug report with Swift here: https://bugs.swift.org/browse/SR-9598)
from swift-snapshot-testing.
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)
- Timeout Error During Snapshot Testing with WKWebView
- Snapshots tests fail when Json returns in different order, HOT 1
- macCatalyst: Snapshots generated differ a lot between local computer and a MacStadium M1 mac mini HOT 1
- Run success on local and xcode on CI Runner but test failed when using fastlane run_tests HOT 5
- Text-based snapshots break across platforms due to line ending differences HOT 2
- Undefined symbols Linker command failed with exit code 1. HOT 1
- Snapshot adds imported module's name that fails match with reference shapshot
- Deadlock in inline snapshot testing HOT 4
- What's the way to use `assertSnapshot` in async test? HOT 3
- How to snapshot on special iOS version? HOT 1
- Optimizing Image Handling in Swift Snapshot Testing
- Snapshot of table view with combine receiver happens too late HOT 1
- swift-syntax 510.0.0 update not possible HOT 1
- How to generate iPhone 15 Snapshots with Xcode 15, as view.swift doesn't contain those dimensions HOT 1
- I get "Process completed with exit code 65" when I use "assertSnapshot(of: vc, as: .image)" function on GitHub Actions.
- `diffTool` have a Swift 6 warning on Xcode 15.3 with Strict Concurrency Checking set to Complete HOT 3
- Privacy manifest for AppStore releases HOT 1
- "The file “...” couldn’t be opened because you don’t have permission to view it" after switching branch
- Privacy manifest file / Privacy Rules HOT 1
- Blank diff snapshot
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift-snapshot-testing.