首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >项目中未解析`runBlocking`协程构建器(解析其他构建器)

项目中未解析`runBlocking`协程构建器(解析其他构建器)
EN

Stack Overflow用户
提问于 2019-03-01 14:18:10
回答 1查看 1.6K关注 0票数 1

正如标题所暗示的那样,我刚刚在build.gradle中添加的协程程序库中缺少协程构建器runBlocking。有趣的是,其他所有的东西似乎都是可用的,GlobalScopeCoroutineScope.launch CoroutineScope.async都存在。runBlocking并非如此,我哪里做错了?

这是我的build.gradle

代码语言:javascript
复制
buildscript {
    ext {
        ktor_version = "1.1.1"
        kotlin_version = "1.3.20-eap-52"
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.44"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
    }
}

plugins {
    id 'kotlin-multiplatform' version '1.3.20-eap-100'
}

repositories {
    maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
    maven { url 'https://dl.bintray.com/kotlin/kotlin-js-wrappers' }
    maven { url 'https://dl.bintray.com/kotlinx/kotlinx' }
    maven { url "https://kotlin.bintray.com/kotlinx" }
    jcenter()
    mavenCentral()
}

group 'books'
version '0.0.0'

apply plugin: 'maven-publish'
apply plugin: "org.jetbrains.kotlin.frontend"

kotlin {
    jvm() {
        compilations.all {
            tasks[compileKotlinTaskName].kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }
    js() {
        compilations.all {
            tasks[compileKotlinTaskName].kotlinOptions {
                def optDir = compileKotlinTaskName.contains("Test") ? "test/${project.name}.test.js" : "main/${project.name}.js"
                kotlinOptions.metaInfo = true
                kotlinOptions.outputFile = "$project.buildDir.path/js/$optDir"
                kotlinOptions.sourceMap = true
                kotlinOptions.moduleKind = 'commonjs'
                kotlinOptions.main = "call"
            }
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$ktor_version"
            }
        }
        commonTest {
            dependsOn commonMain
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        jvmMain {
            dependencies {
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$ktor_version"
                implementation kotlin('stdlib-jdk8')
            }
        }
        jvmTest {
            dependsOn jvmMain
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
            }
        }
        jsMain {
            dependencies {
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$ktor_version"
                implementation kotlin('stdlib-js')
            }
        }
        jsTest {
            dependsOn jsMain
            dependencies {
                implementation kotlin('test-js')
            }
        }
    }
}

task runJest(type: Exec) {
    group = "verification"
    commandLine "sh", "runJest.sh"
}

runJest.dependsOn(jsTest)

task testAll() {
    group = "verification"
    dependsOn(jvmTest, runJest)
}

kotlinFrontend {
    npm {
        devDependency("karma")
    }

    sourceMaps = true

    webpackBundle {
        bundleName = "main"
        host = "0.0.0.0"
        contentPath = file("$buildDir.path/resources/main")
    }
}

有了gradle配置,我已经能够用kotlin-multiplatform很好地编写测试(学习TDD)。下面是我的示例

代码语言:javascript
复制
import kotlin.test.*
import com.luge.books.*
import kotlinx.coroutines.*

class BookTest {
    @BeforeTest
    fun setup() {
        val book = Book()
    }

    @Test
    fun testingInstantiation() {
        val book = Book()
        assertEquals(book.year, 1990, "Books do match the year")
    }

    @Test
    fun willFail() {
        assertFalse(false)
    }

    @Test
    fun testingCoroutines() {
        val job = GlobalScope.launch {
            delay(5000)
            println("Doing stuff")
            assertTrue(false)
        }
    }
}

如果仔细观察,测试testingCoroutines通过了,但因为我是从GlobalScope启动的,所以它只是触发和遗忘,测试返回时不会抛出任何错误。如果我合并runBlocking,集成开发环境会用红色突出显示它(你知道,它不理解的东西),甚至结束kotlin编译器的叫喊,unresolved reference runBlockin。请帮帮忙……

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-01 17:10:32

经过一番努力,我终于知道runBlocking只在kotlin/jvm中可用。因此,它不在kotlin/js或kotlin/common中。

仅供将来参考,如果您想运行多平台测试,则使用此work around

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54938967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档