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
Define function returning an App value? #1147
Comments
use |
I'm already using Anyway, I've noticed other Github issues with the same concern about use actix_http::{Error, Request};
use actix_service::{Service};
use actix_web::dev::{ServiceResponse};
use actix_web::{middleware, test, App};
use actix_web::body::MessageBody;
pub fn get_app() -> impl Service<
Request=Request,
Response=ServiceResponse<impl MessageBody>,
Error=Error,
> {
test::init_service(
App::new()
.configure(/* Add your config handler here. */)
.wrap(middleware::Logger::default()),
)
} Then it's reusable, say, like so: use actix_service::Service;
use actix_web::http::StatusCode;
use actix_web::test::{block_on, TestRequest};
#[test]
fn test_some_route() {
let mut app = get_app();
let req = TestRequest::get().uri("/some/route").to_request();
let rsp = block_on(app.call(req)).unwrap();
assert_eq!(rsp.status(), StatusCode::OK);
} This compiles, allows me to have the |
It is not possible in any case. |
The function signature to accomplish this in main code can look something like this: fn create_app() -> App<
impl ServiceFactory<
ServiceRequest,
Config = (),
Response = ServiceResponse<impl MessageBody>,
Error = Error,
InitError = (),
>,
> {
App::new()
} which can then be used by inside the |
Is it possible to define a function returning an
App
value?Something like this:
I find no examples online of anything like this.
Without this, I don't think it's possible to have nice tests.
Each test would need to construct the
App
value by hand instead of being able to call a function to get it or better yet use a fixture so nice teardown is available as well. MyApp
has multiple middlewares and some of them (likeŀogger
) use types that seem to be private (likeStreamLog
). I know about.configure
and I use it, but it doesn't help with having to set up middlewares in every test.The text was updated successfully, but these errors were encountered: