default-test 0.1.1

A default trait that can be used in tests
Documentation
  • Coverage
  • 50%
    3 out of 6 items documented3 out of 5 items with examples
  • Size
  • Source code size: 11.83 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 354.22 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Homepage
  • austinjones/default-test-rs
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • austinjones

default-test-rs

Provides a Rust trait similar to Default that can be used in tests.

Often tests need to construct mock instances of structs. For example:

struct User {
    id: usize,
    name: String,
    email: String,
    admin: bool
}

While it's tempting to define Default, often tests need mock values for types that shouldn't apply in production code. Sometimes, tests need values for types that don't even implement Default.

This crate provides DefaultTest, a trait which can provide default instances with mock values.

impl DefaultTest for User {
    fn default_test() -> Self {
        User {
            id: 0,
            name: "name".into(),
            email: "email".into(),
            admin: false
        }
    }
}

Unit tests can then use the spread operator to construct values:

mod tests {
    #[test]
    fn test() {
        let user = User {
            id: 99
            ..User::test_default()
        };
        // ...
    }
}

Roadmap:

  • Derive macro which fills sensible defaults that would be useful in unit test implementations. String files would be filled with their property name, and other types may use T::default() or unique values.