首页 > 解决方案 > 使用 WebAssembly 在 Rust 中进行单元测试

问题描述

我正在WASM使用seed框架编写前端。

我想用单元测试测试我的一个模块,但我不能让它们测试WASM代码。这是我的测试套件:

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn check_non_wasm() {
        assert_eq!("test", "test");
    }

    #[test]
    fn check_home() {
        assert_eq!(Urls::new().home().to_string(), "/");
    }
}

第一个测试通过,因为没有使用任何与WASM. 另一个测试失败并出现以下错误:

thread 'urls::tests::check_home' panicked at 'cannot call wasm-bindgen imported functions on non-wasm targets'

如果我运行cargo make test --chrome它,它甚至不会看到这些测试。
我尝试运行cargo test --target wasm32-unknown-unknown,但失败并出现以下错误:

could not execute process `C:\Users\djenty\w\frontend\target\wasm32-unknown-unknown\debug\deps\w_frontend-2b6de747c6501f70.wasm` (never executed)

rustup显示目标已安装:

installed targets for active toolchain
--------------------------------------

wasm32-unknown-unknown
x86_64-pc-windows-msvc

active toolchain
----------------

stable-x86_64-pc-windows-msvc (default)
rustc 1.54.0 (a178d0322 2021-07-26)

我在这里想念什么?

标签: unit-testingrustwebassemblywasm-bindgenrust-wasm

解决方案


推荐阅读