Skip to content

Example from testing docs has compile warning and does not pass #489

@Craig-Macomber

Description

@Craig-Macomber

Problem

Example test code from https://dioxuslabs.com/learn/0.6/cookbook/testing# has compile warning and does not pass

Steps To Reproduce

Steps to reproduce the behavior:

Expected behavior

Example test compiles without warnings and passe

Actual Behavior

thread::Scope import is unused.

running 1 test
test test ... FAILED

successes:

successes:

failures:

---- test stdout ----

thread 'test' panicked at src/main.rs:24:17:
assertion left == right failed
left: 1
right: 4

Environment:

  • Dioxus version: 6.3
  • Rust version: 1.85.0
  • OS info: linux
  • App platform: desktop

Proposed solution

Remove the unused input, and fix the test. I'm not sure whats wrong with it: that example is complex enough that its not obvious to me.

I'd also recommend adding a simpler example for cases which don't require testing mutations. Something like:

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

    fn assert_rsx_eq(first: Element, second: Element) {
        let first = dioxus_ssr::render_element(first);
        let second = dioxus_ssr::render_element(second);
        pretty_assertions::assert_str_eq!(first, second);
    }

    fn test_hook_simple(mut check: impl FnMut() + 'static) {
        fn mock_app() -> Element {
            rsx! {
                div {}
            }
        }

        let vdom = VirtualDom::new(mock_app);

        vdom.in_runtime(|| {
            ScopeId::ROOT.in_runtime(|| {
                check();
            })
        })
    }

    #[test]
    fn example() {
        test_hook_simple(|| {
            // Example use of a hook which requires the Dioxus runtime to be present
            let signal = use_signal(|| "Hello".to_string());
            assert_rsx_eq(
                rsx! {
                    span {
                        {signal}
                        " world"
                    }
                },
                rsx! {
                    span { "Hello world" }
                },
            )
        });
    }
}

This examples uses a test utility I wrote and used for some actual testing in a real library, and proved quite useful.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingdocumentationImprovements or additions to documentation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions