Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there an alternative to this .await syntax? #157

Closed
iot-resister opened this issue Jul 9, 2022 · 1 comment · Fixed by #186
Closed

Is there an alternative to this .await syntax? #157

iot-resister opened this issue Jul 9, 2022 · 1 comment · Fixed by #186

Comments

@iot-resister
Copy link

iot-resister commented Jul 9, 2022

    #[fixture]
    async fn client() -> Client {
        Client::tracked(app()).await.unwrap()
    }

    #[rstest]
    async fn it_can_return_its_ip(#[future] client: Client) {
        let client = client.await;  // <------  Do I need to write this for every test? Can I make it a one liner?
        let resp = client.get("/").dispatch().await;
        assert_eq!("99.73.227.46", resp.into_string().await.unwrap());
    }
@la10736
Copy link
Owner

la10736 commented Jul 10, 2022

I guess that what you want is always a concrete client to use in your tests. What you can do is always block your thread to wait the future is terminated:

    #[fixture]
    fn client_complete(#[future] client: Client) -> Client {
        async_std::task::block_on(client)
    }

or inline the two calls is you never need of a future client

    #[fixture]
    fn client() -> Client {
        async_std::task::block_on( async{ Client::tracked(app()).await.unwrap() })
    }

The block syntax depend from your runtime ... I've used async-std.

Finally if you need just a client to share in all your tests you can use #[once] for your fixture... but I don't know if this have some sense in your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants