[−][src]Attribute Macro faux::create
#[create]
Transforms the given struct into a mockable version of itself.
It creates an associated function for the tagged struct called
faux
and masks the original definition of the struct by changing
its name.
Use cargo-expand to see what your struct expands to after the macro.
Requirements
This macro deliberately fails to compile if any of the struct's fields are not private. Otherwise, a user of the struct could try to access the field directly when it no longer exists in the transformed version.
The transformed struct is useless unless its methods are also
mocked. See #[methods] for documentation on how to mock the
methods of the struct. If #[methods]
is not used for an impl
block, methods inside the impl may not use any of its fields.
Usage
#[faux::create] pub struct MyStruct { a: i32, b: Vec<u32>, } #[faux::methods] impl MyStruct { /* methods go here */ } // creates a mock out of MyStruct let my_mock = MyStruct::faux();
Attribute arguments
self_type
Allowed values:
#[methods(self_type = "Rc")]
#[methods(self_type = "Arc")]
#[methods(self_type = "Box")]
#[methods(self_type = "Owned")]
- this is the default and not necessary
This argument tells faux that instead of holding a real value of your
struct (when the struct is not being mocked), to hold your struct
wrapped in the specified self_type
. Faux should guide you
towards using this argument through either a compile error or a
panic in your tests.
This argument is helpful when you want your struct to ONLY be
publicly viewed through one of these wrappers (i.e., your
functions to create your struct returns an Rcself_type
is specified here, then it must also be specified in
all of the impl blocks tagged with #[methods]. The author's
recommendation is to only use this feature when faux suggests you
to.
Usage
use std::sync::Arc; #[faux::create(self_type = "Arc")] pub struct MyStruct { /* private fields */ } #[faux::methods(self_type = "Arc")] impl MyStruct { pub fn new() -> Arc<Self> { /* implementation */ } /* more methods */ }