Hi.
I made an example of integration testing for Hyper with helper function which run a server and execute a test, which passed as parameter.
async fn with_server<T, R>(f: T) -> Result<(), AppError>
where
T: Fn(SocketAddr) -> R,
R: Future<Output = Result<(), AppError>>,
{
...
tokio::spawn(server);
f(server_addr).await?;
Ok(())
}
#[tokio::test]
async fn ok_test() -> Result<(), AppError> {
with_server(|server_addr| async {
let client = Client::new();
let uri = format!("http://{}/", server_addr).parse().unwrap();
let resp = client.get(uri).await?;
assert_eq!(StatusCode::OK, resp.status());
Ok(())
})
.await
}
But i got an error about borrowing:
error[E0515]: cannot return value referencing function parameter `server_addr`
--> tests/foo_test.rs:35:31
|
35 | with_server(|server_addr| async {
| _______________________________^
36 | | let client = Client::new();
37 | | let uri = format!("http://{}/", server_addr).parse().unwrap();
| | ----------- `server_addr` is borrowed here
38 | |
... |
42 | | Ok(())
43 | | })
| |_____^ returns a value referencing data owned by the current function
Could anybody help me to understand where is a problem? I don't return any value from this test function. Or i'm return a value?