The Race test kit makes unit testing straightforward. You can:
Create a test client: Instantiate a TestClient object to simulate a player or server.
// Rust exampleuse race_test::TestClient;use race_core::types::ClientMode;// Create a test client to simulate a playerletmut player =TestClient::player("Alice");// Create a test client to simulate a transactor serverletmut transactor =TestClient::transactor("Foo");
Build a test game account: Use the TestGameAccountBuilder to create a mock game account with the desired initial state and configuration.
// Rust exampleuse race_test::TestGameAccountBuilder;// Define your game's account data structure#[derive(BorshSerialize, BorshDeserialize)]structMyGameData {// ... game-specific data fields}// Create a test game account with initial data and configurationlet game_account =TestGameAccountBuilder::default().add_player(&mut player, 1000) // Add a player with initial balance.set_transactor(&mut transactor) // Set the transactor server.with_data(MyGameData { /* ... */ }) // Provide initial game data.build();
Initialize the game context and handler: Create a GameContext object based on the test game account and initialize your game handler using the TestHandler.
// Rust exampleuse race_core::context::GameContext;use race_test::TestHandler;// Create the game contextletmut ctx =GameContext::try_new(&game_account).unwrap();// Initialize your game handler (replace MyGameHandler with your actual handler)letmut handler =TestHandler::<MyGameHandler>::init_state(&mut ctx, &game_account).unwrap();
Send events and observe outcomes: Use the test client to send custom and built-in events to the game handler. Assert that the game state and generated effects match your expectations.
// Rust exampleuse race_api::event::Event;// Create a custom game event (replace MyCustomEvent with your event type)let custom_event =Event::custom(player.id(), &MyCustomEvent { /* ... */ });// Send the event to the game handlerhandler.handle_event(&mut ctx, &custom_event).unwrap();// Assert that the game state and effects are as expectedassert_eq!(ctx.get_status(), GameStatus::Running); // Example assertion// ... more assertions
Remember:
Replace MyGameHandler and MyCustomEvent with your actual game handler and custom event types.
Write specific assertions to verify the expected behavior of your game logic after handling events.
By following these steps, you can effectively unit test your Race Protocol game bundles using the Race test kit.
Examples
WASM Bundle (Rust)
Here's an example of unit testing a custom event handler within the WASM bundle using the Race test kit in Rust:
use race_test::{TestClient, TestGameAccountBuilder, TestHandler};let game_account =TestGameAccountBuilder::new().build();let context =GameContext::new(game_account);let handler =TestHandler::new(game_bundle, encryptor, client, decryption_cache);letmut test_client =TestClient::new(context, handler);// Send a custom event and assert the expected state updatetest_client.submit_event(CustomEvent { ... });let updated_state = test_client.context().handler_state();assert_eq!(updated_state, expected_state);
This code snippet demonstrates how to use the test kit in Rust to create a test client, build a mock game account, and send a custom event to the game handler. The test then asserts that the resulting game state matches the expected outcome.
Client-Side (TypeScript)
Here's an example of unit testing in TypeScript using Race JS SDK
import { TestClient, TestGameAccountBuilder, TestHandler } from'@race-foundation/sdk-core';constgameAccount=newTestGameAccountBuilder().build();constcontext=newGameContext(gameAccount);consthandler=newTestHandler(gameBundle, encryptor, client, decryptionCache);consttestClient=newTestClient(context, handler);// Send a custom event and assert the expected state updateawaittestClient.submitEvent({ type:'playerAction',... });constupdatedState=context.handlerState;assert.deepEqual(updatedState, expectedState);
This code snippet demonstrates how to use the test kit to create a test client, build a mock game account, and send a custom event to the game handler. The test then asserts that the resulting game state matches the expected outcome.