对于我的单元测试,我需要导入 kotlin.test 才能使用 assertFailsWith。
我相信我的应用程序 build.grade 文件中有正确的文件。文件的一部分是:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
implementation 'androidx.navigation:navigation-fragment:2.0.0'
implementation 'androidx.navigation:navigation-ui:2.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
// ----
// Per https://developer.android.com/training/testing/unit-testing/local-unit-tests
//
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.0.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:1.10.19'
// ---- …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个 func,它 1) 实例化 UIViewController 的子类和 2) 推送到调用方 UIViewController 的导航控制器中。
到目前为止,我有这个:
func pushAnyViewController<T>(viewController:T, storyboardName:String) {
// Instantiate the view controller of type T
guard let nextViewController = UIStoryboard(name: storyboardName, bundle: nil).instantiateViewController(withIdentifier: String(describing: T.self)) as? T else { return }
viewController.navigationController.pushViewController(nextViewController, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
这会产生错误
Value of type 'T' has no member 'navigationController'
我不确定是否应该说 T 将始终是 UIViewController 的子类。如果是这样的话,我不清楚我在哪里这样做。为此,我想过:
func pushAnyViewController<T>(viewController:T & UIViewController, storyboardName:String)
Run Code Online (Sandbox Code Playgroud)
但这会产生错误:
Generic parameter 'T' is not used in function signature
和
Non-protocol, non-class type 'T' cannot be used …