tide icon indicating copy to clipboard operation
tide copied to clipboard

Unit testing example

Open vladinator1000 opened this issue 4 years ago • 2 comments

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?

vladinator1000 avatar Apr 24 '21 15:04 vladinator1000

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

Fishrock123 avatar Apr 28 '21 06:04 Fishrock123

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)

medwards avatar May 17 '22 14:05 medwards