pub trait TestApp: Sealed {
Show 13 methods
// Required methods
fn spawn_empty(&mut self) -> EntityWorldMut<'_>;
fn spawn<B: Bundle>(&mut self, bundle: B) -> EntityWorldMut<'_>;
fn spawn_batch<I>(&mut self, iter: I) -> SpawnBatchIter<'_, I::IntoIter>
where I: IntoIterator,
I::Item: Bundle;
fn entity(&self, entity: Entity) -> EntityRef<'_>;
fn entity_mut(&mut self, entity: Entity) -> EntityWorldMut<'_>;
fn get_entity(&self, entity: Entity) -> Option<EntityRef<'_>>;
fn get_entity_mut(&mut self, entity: Entity) -> Option<EntityWorldMut<'_>>;
fn component<T: Component>(&self, entity: Entity) -> &T;
fn get_component<T: Component>(&self, entity: Entity) -> Option<&T>;
fn query<'w, D: ReadOnlyQueryData>(&'w mut self) -> AssertQuery<'w, D>
where D::Item<'w>: PartialEq + Debug;
fn query_filtered<'w, D: ReadOnlyQueryData, F: QueryFilter>(
&'w mut self,
) -> AssertQuery<'w, D>
where D::Item<'w>: PartialEq + Debug;
fn update_once(&mut self);
fn update_n_times(&mut self, amount: u32);
}Required Methods§
Sourcefn spawn_empty(&mut self) -> EntityWorldMut<'_>
fn spawn_empty(&mut self) -> EntityWorldMut<'_>
Spawns a new Entity and returns a corresponding EntityWorldMut, which can be used
to add components to the entity or retrieve its id.
use bevy_testing::p::*;
#[derive(Component)]
struct Position {
x: f32,
y: f32,
}
#[derive(Component)]
struct Label(&'static str);
#[derive(Component)]
struct Num(u32);
let mut app = App::new();
let entity = app.spawn_empty()
.insert(Position { x: 0.0, y: 0.0 }) // add a single component
.insert((Num(1), Label("hello"))) // add a bundle of components
.id();
let position = app.component::<Position>(entity);
assert_eq!(position.x, 0.0);Sourcefn spawn<B: Bundle>(&mut self, bundle: B) -> EntityWorldMut<'_>
fn spawn<B: Bundle>(&mut self, bundle: B) -> EntityWorldMut<'_>
Spawns a new Entity with a given Bundle of components and returns
a corresponding EntityWorldMut, which can be used to add components to the entity or
retrieve its id.
use bevy_testing::p::*;
#[derive(Component)]
struct Position {
x: f32,
y: f32,
}
#[derive(Component)]
struct Velocity {
x: f32,
y: f32,
};
#[derive(Component)]
struct Name(&'static str);
#[derive(Bundle)]
struct PhysicsBundle {
position: Position,
velocity: Velocity,
}
let mut app = App::new();
// `spawn` can accept a single component:
app.spawn(Position { x: 0.0, y: 0.0 });
// It can also accept a tuple of components:
app.spawn((
Position { x: 0.0, y: 0.0 },
Velocity { x: 1.0, y: 1.0 },
));
// Or it can accept a pre-defined Bundle of components:
app.spawn(PhysicsBundle {
position: Position { x: 2.0, y: 2.0 },
velocity: Velocity { x: 0.0, y: 4.0 },
});
let entity = app
// Tuples can also mix Bundles and Components
.spawn((
PhysicsBundle {
position: Position { x: 2.0, y: 2.0 },
velocity: Velocity { x: 0.0, y: 4.0 },
},
Name("Elaina Proctor"),
))
// Calling id() will return the unique identifier for the spawned entity
.id();
let position = app.component::<Position>(entity);
assert_eq!(position.x, 2.0);Sourcefn spawn_batch<I>(&mut self, iter: I) -> SpawnBatchIter<'_, I::IntoIter>
fn spawn_batch<I>(&mut self, iter: I) -> SpawnBatchIter<'_, I::IntoIter>
Spawns a batch of entities with the same component Bundle type. Takes a given
Bundle iterator and returns a corresponding Entity iterator.
This is more efficient than spawning entities and adding components to them individually,
but it is limited to spawning entities with the same Bundle type, whereas spawning
individually is more flexible.
use bevy_testing::p::*;
#[derive(Component)]
struct Str(&'static str);
#[derive(Component)]
struct Num(u32);
let mut app = App::new();
let entities = app.spawn_batch(vec![
(Str("a"), Num(0)), // the first entity
(Str("b"), Num(1)), // the second entity
]).collect::<Vec<Entity>>();
assert_eq!(entities.len(), 2);Sourcefn entity(&self, entity: Entity) -> EntityRef<'_>
fn entity(&self, entity: Entity) -> EntityRef<'_>
Retrieves an EntityRef that exposes read-only operations for the given entity.
This will panic if the entity does not exist. Use TestApp::get_entity if you want
to check for entity existence instead of implicitly panic-ing.
If you want to get a specific component from an entity, use TestApp::component.
use bevy_testing::p::*;
#[derive(Component)]
struct Position {
x: f32,
y: f32,
}
let mut app = App::new();
let entity = app.spawn(Position { x: 0.0, y: 0.0 }).id();
let position = app.entity(entity).get::<Position>().unwrap();
// preferred:
// let position = world.component::<Position>(entity)
assert_eq!(position.x, 0.0);Sourcefn entity_mut(&mut self, entity: Entity) -> EntityWorldMut<'_>
fn entity_mut(&mut self, entity: Entity) -> EntityWorldMut<'_>
Retrieves an EntityWorldMut that exposes read and write operations for the given entity.
This will panic if the entity does not exist. Use TestApp::get_entity_mut if you want
to check for entity existence instead of implicitly panic-ing.
use bevy_testing::p::*;
#[derive(Component)]
struct Position {
x: f32,
y: f32,
}
let mut app = App::new();
let entity = app.spawn(Position { x: 0.0, y: 0.0 }).id();
let mut entity_mut = app.entity_mut(entity);
let mut position = entity_mut.get_mut::<Position>().unwrap();
position.x = 1.0;
let new_position = app.component::<Position>(entity);
assert_eq!(new_position.x, 1.0);
Sourcefn get_entity(&self, entity: Entity) -> Option<EntityRef<'_>>
fn get_entity(&self, entity: Entity) -> Option<EntityRef<'_>>
Retrieves an EntityRef that exposes read-only operations for the given entity.
Returns None if the entity does not exist.
Instead of unwrapping the value returned from this function, prefer TestApp::entity.
If you want to get a specific component from an entity, use TestApp::get_component.
use bevy_testing::p::*;
#[derive(Component)]
struct Position {
x: f32,
y: f32,
}
let mut app = App::new();
let entity = app.spawn(Position { x: 0.0, y: 0.0 }).id();
let entity_ref = app.get_entity(entity).unwrap();
let position = entity_ref.get::<Position>().unwrap();
// preferred:
// let position = world.component::<Position>(entity)
assert_eq!(position.x, 0.0);Sourcefn get_entity_mut(&mut self, entity: Entity) -> Option<EntityWorldMut<'_>>
fn get_entity_mut(&mut self, entity: Entity) -> Option<EntityWorldMut<'_>>
Retrieves an EntityWorldMut that exposes read and write operations for the given entity.
Returns None if the entity does not exist.
Instead of unwrapping the value returned from this function, prefer TestApp::entity_mut.
use bevy_testing::p::*;
#[derive(Component)]
struct Position {
x: f32,
y: f32,
}
let mut app = App::new();
let entity = app.spawn(Position { x: 0.0, y: 0.0 }).id();
let mut entity_mut = app.get_entity_mut(entity).unwrap();
let mut position = entity_mut.get_mut::<Position>().unwrap();
position.x = 1.0;
let new_position = app.component::<Position>(entity);
assert_eq!(new_position.x, 1.0);Sourcefn component<T: Component>(&self, entity: Entity) -> &T
fn component<T: Component>(&self, entity: Entity) -> &T
Gets access to the component of type T for the given entity.
Panics if the entity doesn’t have a component of type T or
if the entity doesn’t exist.
This is effectively a shortcut for App::entity(entity).get::<T>().unwrap().
use bevy_testing::p::*;
#[derive(Component)]
struct Position {
x: f32,
y: f32,
}
let mut app = App::new();
let entity = app.spawn(Position { x: 0.0, y: 0.0 }).id();
let position = app.component::<Position>(entity);
assert_eq!(position.x, 0.0);Sourcefn get_component<T: Component>(&self, entity: Entity) -> Option<&T>
fn get_component<T: Component>(&self, entity: Entity) -> Option<&T>
Gets access to the component of type T for the given entity.
Returns None if the entity doesn’t have a component of type T.
Panics if the entity doesn’t exist.
This is effectively a shortcut for App::entity(entity).get::<T>().
Instead of unwrapping the value returned from this function, prefer TestApp::entity.
use bevy_testing::p::*;
#[derive(Component)]
struct Position {
x: f32,
y: f32,
}
let mut app = App::new();
let entity = app.spawn(Position { x: 0.0, y: 0.0 }).id();
let position = app.get_component::<Position>(entity).unwrap();
// preferred:
// let position = world.component::<Position>(entity)
assert_eq!(position.x, 0.0);Sourcefn query<'w, D: ReadOnlyQueryData>(&'w mut self) -> AssertQuery<'w, D>
fn query<'w, D: ReadOnlyQueryData>(&'w mut self) -> AssertQuery<'w, D>
Returns an [AssertQuery] which can be used to perform tests on a query.
To invert the test, use [AssertQuery::not].
If you need to use a query filter, use App::query_filtered.
use bevy_testing::p::*;
#[derive(Component, Debug, PartialEq)]
struct Position {
x: f32,
y: f32,
}
let mut app = App::new();
app.spawn(Position { x: 0.0, y: 0.0 });
app.spawn(Position { x: 1.0, y: 2.0 });
app.spawn(Position { x: 4.5, y: 1.0 });
app.query::<&Position>()
.has(&Position { x: 1.0, y: 2.0 })
.not().has(&Position { x: 4.0, y: -3.0 })
.length(3);Sourcefn query_filtered<'w, D: ReadOnlyQueryData, F: QueryFilter>(
&'w mut self,
) -> AssertQuery<'w, D>
fn query_filtered<'w, D: ReadOnlyQueryData, F: QueryFilter>( &'w mut self, ) -> AssertQuery<'w, D>
Returns an [AssertQuery] which can be used to perform tests on a query, with a query filter.
To invert the test, use [AssertQuery::not].
If you don’t need the filter, use App::query.
Note that some filters such as Changed might behave unexpectedly.
use bevy_testing::p::*;
#[derive(Component, Debug, PartialEq)]
struct Position {
x: f32,
y: f32,
}
#[derive(Component, Debug, PartialEq)]
struct Marker;
let mut app = App::new();
app.spawn((Position { x: 0.0, y: 0.0 }, Marker));
app.spawn((Position { x: 1.0, y: 2.0 }, Marker));
app.spawn(Position { x: 4.5, y: 1.0 });
app.query_filtered::<&Position, With<Marker>>()
.has(&Position { x: 1.0, y: 2.0 })
.not().has(&Position { x: 4.5, y: 1.0 })
.length(2);Sourcefn update_once(&mut self)
fn update_once(&mut self)
Updates the app once.
This will run all of the main schedules such as Update and FixedUpdate,
along with Startup if it’s the first update.
If you want to update the app multiple times, use App::update_n_times.
use my_lib::{Countdown, CountdownPlugin};
use bevy_testing::p::*;
let mut app = App::new();
app.add_plugins(CountdownPlugin);
app.spawn(Countdown(10));
app.update_once();
app.query::<&Countdown>()
.matches(vec![&Countdown(9)]);Sourcefn update_n_times(&mut self, amount: u32)
fn update_n_times(&mut self, amount: u32)
Updates the app amount times.
This will run all of the main schedules such as Update and FixedUpdate,
along with Startup if it’s the first update.
If you want to update the app just once, use App::update_n_times.
use my_lib::{Countdown, CountdownPlugin};
use bevy_testing::p::*;
let mut app = App::new();
app.add_plugins(CountdownPlugin);
app.spawn(Countdown(10));
app.update_n_times(2);
app.query::<&Countdown>()
.matches(vec![&Countdown(8)]);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.