What's the correct way to test these? Getting a couple of examples will be great. Thanks.
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"You can create RootNode with an EmptyMutation and EmptySubscription and run execute on it. We do it like that in integration tests:
pub struct DemoQuery {}\n\n#[graphql_object(context = Context)]\nimpl DemoQuery {\n pub fn outputs(input: Input) -> Vec<Output> {\n vec![Output {\n id: input.id\n }]\n }\n}\n\n#[cfg(test)]\nmod tests {\n use juniper::{execute, graphql_value, graphql_vars, EmptyMutation, EmptySubscription, RootNode}\n\n use super::*;\n\n #[tokio::test]\n async fn resolves() {\n const QUERY: &str = r#\"{\n outputs(input: ...) {\n id\n }\n }\"#;\n\n let schema = RootNode::new(\n DemoQuery,\n EmptyMutation::new(),\n EmptySubscription::new(),\n );\n\n assert_eq!(\n execute(QUERY, None, &schema, &graphql_vars! {}, &()).await,\n Ok((\n graphql_value!({\"humanString\": {\"id\": \"mars\", \"planet\": \"mars\"}}),\n /* expected output */,\n )),\n );\n }\n}-
|
Hello, I'm new to juniper and I'm loving it so far. I want to write tests but I don't know how to go around that. pub struct DemoQuery {}
#[graphql_object(context = Context)]
impl DemoQuery {
pub fn outputs(input: Input) -> Vec<Output> {
vec![Output {
id: input.id
}]
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test_outputs() {
let query = DemoQuery {};
let input = Input { id: 12 };
query.outputs(input); // but we can't access it like this
}
}What's the correct way to test these? Getting a couple of examples will be great. Thanks. |
Beta Was this translation helpful? Give feedback.
-
|
You can create pub struct DemoQuery {}
#[graphql_object(context = Context)]
impl DemoQuery {
pub fn outputs(input: Input) -> Vec<Output> {
vec![Output {
id: input.id
}]
}
}
#[cfg(test)]
mod tests {
use juniper::{execute, graphql_value, graphql_vars, EmptyMutation, EmptySubscription, RootNode}
use super::*;
#[tokio::test]
async fn resolves() {
const QUERY: &str = r#"{
outputs(input: ...) {
id
}
}"#;
let schema = RootNode::new(
DemoQuery,
EmptyMutation::new(),
EmptySubscription::new(),
);
assert_eq!(
execute(QUERY, None, &schema, &graphql_vars! {}, &()).await,
Ok((
graphql_value!({"humanString": {"id": "mars", "planet": "mars"}}),
/* expected output */,
)),
);
}
} |
Beta Was this translation helpful? Give feedback.
You can create
RootNodewith anEmptyMutationandEmptySubscriptionand runexecuteon it. We do it like that in integration tests: