2

Rust newbie here (<7 days into learning), the second hurdle I am trying to overcome after ownership rule is async/await.

I am writing a test that calls an async function and I need to get the result from the Future without using the keyword await.

I have looked at async_test, however I can't use that because (as I understand) this requires tokio runtime and #[tokio_main] attribute in my main method - but I have my main already decorated with #[actix_rt::main]

This is my test

#[test]
pub fn test_get_item() -> Result<(), anyhow::Error> {
    let c = SomeClient::new();
    let result = c.get_item(123456).await?; // <- this is not allowed

    assert_eq!("Hello", result.title);
    assert_eq!("https://example.com", result.url.as_str());

    Ok(())
}

Things I have tried and failed (mostly due to my lack of knowledge in Rust)

I don't understand why this has to be so complicated, maybe there is a simple function (like wait() or get_result()) somewhere for Future?

Thanks for your help.

undefined
  • 2,939
  • 4
  • 23
  • 35

1 Answers1

1

Had to clear my head for a bit and start again, just to find actix-web has a macro I can use that allows async tests.

#[actix_rt::test]

Macros are all magic to me at this point, I hope I;ll understand them soon.

undefined
  • 2,939
  • 4
  • 23
  • 35