FluentFieldAssertions
FluentFieldAssertions is a library that allows you to write tests in a natural language-like syntax.
With this library, you can perform field assertions in an intuitive and readable way.
Features
- Natural Language Syntax: You can write test code in a syntax that closely resembles natural language.
- Method Chaining: You can chain assertions as method calls, allowing you to express multiple assertions in a single statement.
- Readability and Maintainability: The code becomes simple and readable, improving the maintainability of your tests.
Uses
FluentFieldAssertions generated the following methods for each field.
fn {field}_eq(&self, expected: {field_type}) -> &Self- Asserts that the field is equal to the expected value.
fn {field}_ne(&self, expected: {field_type}) -> &Self- Asserts that the field is not equal to the expected value.
fn {field}_satisfies(&self, pred: impl FnOnce(&{field_type}) -> bool) -> &Self- Asserts that the field satisfies the predicate.
use FluentFieldAssertions;
let user = User ;
// You can write tests in a natural language-like syntax.
user.id_eq
.name_eq
.age_satisfies;
// Same as above.
assert_eq!;
assert_eq!;
assert!;
Examples
Basic struct
You can use it in the basic struct.
Each field must implement the traits Eq and Debug.
use FluentFieldAssertions;
let user = User ;
// You can use `{field}_eq` to assert_eq!.
user.name_eq;
assert_eq!; // Same as above.
// You can use `{field}_ne` to assert_ne!.
user.name_ne;
assert_ne!; // Same as above.
// You can use `{field}_satisfies` to assert!.
user.name_satisfies;
assert!; // Same as above.
// You can chain assertions as method calls.
user.id_eq
.name_eq
.age_satisfies;
// Same as above.
assert_eq!;
assert_eq!;
assert!;
Generic struct
You can also use it in the generic struct.
In that case, Generics type T must implement the traits Eq and Debug.
use Debug;
use FluentFieldAssertions;
let point = Point ;
point.x_eq.y_ne.z_satisfies;