在这里生锈新手(不到7天的学习时间),这是所有权规则为异步/等待后我要克服的第二个障碍。
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]
这是我的考验
#[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(())
}
我尝试过和失败的事情(主要是由于我对Rust缺乏了解)
- Use
async_test
on an actix web project using futures-await-test crate. - Read this thread in reddit.
- Follow few examples from this rust community thread
- Tried to
poll()
the future but it didn't lead me anywhere.
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
?
谢谢你的帮助。
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.在这一点上,宏对我来说都是魔术,我希望我能尽快理解它们。