pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: &str)where
    T: Serialize + ?Sized,
Expand description

Asserts that value serializes to the given tokens, and then yields error.

use serde_derive::Serialize;
use serde_test::{assert_ser_tokens_error, Token};
use std::sync::{Arc, Mutex};
use std::thread;

struct Example {
    lock: Arc<Mutex<u32>>,

fn main() {
    let example = Example {
        lock: Arc::new(Mutex::new(0)),
    let lock = example.lock.clone();

    let thread = thread::spawn(move || {
        // This thread will acquire the mutex first, unwrapping the result
        // of `lock` because the lock has not been poisoned.
        let _guard = lock.lock().unwrap();

        // This panic while holding the lock (`_guard` is in scope) will
        // poison the mutex.

    let expected = &[
        Token::Struct {
            name: "Example",
            len: 1,
    let error = "lock poison error while serializing";
    assert_ser_tokens_error(&example, expected, error);