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

Unit testing example #826

Open
vladinator1000 opened this issue Apr 24, 2021 · 2 comments
Open

Unit testing example #826

vladinator1000 opened this issue Apr 24, 2021 · 2 comments

Comments

@vladinator1000
Copy link

vladinator1000 commented Apr 24, 2021

Hello dear Tide community ❤️

I've been trying, to unit test my endpoints but it seems that the stuff I need for that are private. Having an example for this could be super useful for testing side-effects of your endpoints.

Here's an example of what I tried

#[test]
fn endpoint_test() {
    let state = Arc::new(Mutex::new("hello"));
    
    // this associated function is private, doesn't compile
    let http_request = tide::http::Request::new(
        Method::Get,
        Url::parse("http://0.0.0.0/play/sound2.mp3").unwrap(),
    );

    // this is private, how do I fix that?
    let params = vec![tide::request::Params::new()];

    // this is private, doesn't compile
    let req = tide::Request::new(state, http_request, params);

    let result = my_endpoint(req);
    
    // your expected result here
    let expected_result = ();
    assert_eq!(result, expected_result);
}

I see Tide has these methods and structs public only for the crate, what would be a good way to move forward on unit tests here?

@Fishrock123
Copy link
Member

Fishrock123 commented Apr 28, 2021

I suggest looking at https://github.com/eaze/preroll/tree/latest/preroll-example/tests for production-grade "unit-integration" tests. It may be a while until this comes closer to Tide itself. Some of this may also end up in https://github.com/jbr/tide-testing

@medwards
Copy link

I have something like fn get_info(req: &tide::Request<()>) -> InfoEnum that folks want to add tests for, but with no way to create a Request in isolation its pretty raw.

In my own project I would do a test only impl From<(tide::http::Request, State)> for tide::Request but that won't work if its inside tide itself. It would be nice to be able to write unit tests the same as within the project (ie like test/params.rs)

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

No branches or pull requests

3 participants