[−][src]Attribute Macro faux::create
#[create]
Transforms a 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.
Only methods within impl
blocks tagged by #[methods] may use
any of the struct 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:
#[create(self_type = "Rc")]
#[create(self_type = "Arc")]
#[create(self_type = "Box")]
#[create(self_type = "Owned")]
- this is the default and not necessary
Indicates how to wrap the real value of the struct when not being
mocked, e.g., wrap an owned instance vs an Rc<>
. faux
will
guide to use this attribute when needed through either a compile
time error or a panic. Do not use unless faux
asks you to.
Be default faux
wraps owned instance of your struct (i.e.,
MyStruct
). However, sometimes this is not ideal if the only
interactions for this struct are through a different self type
(e.g., self: Rc<Mystruct>
). In this case, we can indicate faux
to hold a non-owned version (e.g., Rc<MyStruct>
). This is
particularly useful if the only methods that return an instance of
the struct return a non-owned instance of it.
If this attribute is set, all of the impl
blocks tagged with
#[methods] must specify the same self_type
.
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 */ }