Skip to content

Instantly share code, notes, and snippets.

@wontheone1
Created May 17, 2020 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wontheone1/5a80471e5dc1e576f1fc243ddfeb859f to your computer and use it in GitHub Desktop.
Save wontheone1/5a80471e5dc1e576f1fc243ddfeb859f to your computer and use it in GitHub Desktop.
Failing actix test
use actix_web::{App, web, get, HttpResponse, HttpServer};
use std::sync::Mutex;
struct AppStateWithCounter {
counter: Mutex<i32>, // <- Mutex is necessary to mutate safely across threads
}
#[get("/")]
async fn index(data: web::Data<AppStateWithCounter>) -> HttpResponse {
let mut counter = data.counter.lock().unwrap(); // <- get counter's MutexGuard
*counter += 1; // <- access counter inside MutexGuard
HttpResponse::Ok().body(format! ("Request number: {}!", counter))
}
#[get("/again")]
async fn index2(data: web::Data<AppStateWithCounter>) -> HttpResponse {
let mut counter = data.counter.lock().unwrap(); // <- get counter's MutexGuard
*counter += 1; // <- access counter inside MutexGuard
HttpResponse::Ok().body(format! ("Request number: {}, again!", counter))
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
let counter = web::Data::new(AppStateWithCounter {
counter: Mutex::new(0),
});
HttpServer::new(move || {
App::new()
.app_data(counter.clone())
.service(index)
.service(index2)
})
.bind("127.0.0.1:8088")?
.run()
.await
}
#[cfg(test)]
mod tests {
use super::*;
use actix_web::{http};
#[actix_rt::test]
async fn test_index_status_code() {
let counter = web::Data::new(AppStateWithCounter {
counter: Mutex::new(0),
});
let resp = index(counter).await;
assert_eq!(resp.status(), http::StatusCode::OK);
}
#[actix_rt::test]
async fn test_index_body() {
let counter = web::Data::new(AppStateWithCounter {
counter: Mutex::new(0),
});
let resp = index(counter).await;
let (body, mut resp) = resp.take_body().into_future().await;
assert_eq!(body, "moi");
}
}
@wontheone1
Copy link
Author

wontheone1 commented May 17, 2020

Error message from test shows

✗ cargo test
   Compiling challenge v0.1.0 (/Users/wseobseo/compensate-api-challenge)
error[E0618]: expected function, found `index`
  --> src/main.rs:50:20
   |
8  | #[get("/")]
   | ----------- `index` defined here
...
50 |         let resp = index(counter).await;
   |                    ^^^^^---------
   |                    |
   |                    call expression requires function

error[E0618]: expected function, found `index`
  --> src/main.rs:60:20
   |
8  | #[get("/")]
   | ----------- `index` defined here
...
60 |         let resp = index(counter).await;
   |                    ^^^^^---------
   |                    |
   |                    call expression requires function

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0618`.
error: could not compile `challenge`.

To learn more, run the command again with --verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment