Keyboard shortcuts

Press or to navigate between chapters

Press ? to show this help

Press Esc to hide this help

ExpectedEvent

The ExpectedEvent trait provides a convenient API for constructing expected events in tests.

This implementation is generated by the #[generate_trait] macro and offers three methods:

  • new(): creates a fresh Event with empty keys and data arrays.
  • key(value): serializes the given value and appends it to the keys array.
  • data(value): serializes the given value and appends it to the data array.

By using these methods, you can construct events in a fluent style without manually managing the keys and data arrays. For example, instead of:

let mut keys = array![];
keys.append_serde(selector!("Transfer")); // event selector
keys.append_serde(from);                  // key #1
keys.append_serde(to);                    // key #2

let mut data = array![];
data.append_serde(value);                 // non-key data
let expected = Event { keys, data };

spy.assert_emitted(
    @array![(contract_address, expected)],
);

you can now write:

let expected = ExpectedEvent::new()
    .key(selector!("Transfer"))
    .key(from)
    .key(to)
    .data(value);
spy.assert_emitted(
    @array![(contract_address, expected)]
);

NOTE: key and data accept any serializable value as input.

Fully qualified path: openzeppelin_testing::events::ExpectedEvent

pub impl ExpectedEvent of ExpectedEventTrait;

Impl functions

new

Creates a new Event with empty keys and data arrays.

Fully qualified path: openzeppelin_testing::events::ExpectedEvent::new

fn new() -> Event

key

Serializes the given value and appends it to the keys array.

Fully qualified path: openzeppelin_testing::events::ExpectedEvent::key

fn key(self: Event, value: T) -> Event

data

Serializes the given value and appends it to the data array.

Fully qualified path: openzeppelin_testing::events::ExpectedEvent::data

fn data(self: Event, value: T) -> Event