#Testing functions that take a `Query` from a World without systems

2 messages · Page 1 of 1 (latest)

upper siren
#

I have some functions that are called from systems, that are passed queries directly with the data they care about.
Their signature looks something like

fn helper(some_query: &Query<&Foo>){}

This is fine and I can pass through the query that I get from my systems to these functions to iterate over them.

However when it comes to adding tests for these functions (and I'd really like to unit test these functions in particular) I'm struggling.
In my test I'd like to create a World to populate with test entities, that I can then query to pass into the function to test its behaviour.
However World::query() returns a QueryState rather than a Query.

Am I missing some more generic type I can give some_query in my helper function to let me both pass through a query from a system, and also directly from a world for testing?

winged maple
#

You can use SystemState to create any parameter a system may take, for example:

let mut state = SystemState::<Query<&Foo>>::new(&mut world);
let query = state.get(&mut world);

You can also create multiple parameters:

let mut state = SystemState::<(Query<&Foo>, Query<&mut Bar>, Res<Baz>)>::new(&mut world);
let (foos, bars, baz) = state.get(&mut world);