nrxus / faux

Struct mocking library for Rust
https://nrxus.github.io/faux/
MIT License
411 stars 14 forks source link

trait support? #54

Open xiaoquanju opened 1 year ago

xiaoquanju commented 1 year ago

good library.!!!!!!!!!

is there plan to support trait ?

xiaoquanju commented 1 year ago

pub trait A{ fn resize(&self, cols: u16, rows: u16) -> Result<(), String>; }

struct MockA{} //i will define a struct

[cfg_attr(test, faux::create)] /

impl A for MockA{ // i dont want to write there dummy code , and need faux auto generate fn resize(&self, cols: u16, rows: u16) -> Result<(), String>{ !todo() }; }

let a = MockA::faux() faux::when!(a.resize) .then_return(Ok(()));

nrxus commented 1 year ago

This is definitely something I want to do, but I haven't prioritized it yet. The hardest part is coming up with something ergonomic that works for all traits. My ideal solution would be something like:

#[cfg_attr(test, faux::create)] 
pub trait MyTrait {
  /* declare methods */
}

#[test]
fn my_test() {
    let mock = MyTrait::faux();
}

But rust only allows calling instance methods directly on traits for object safe traits which means it is not a global solution. Maybe I'll just have to say that it's good enough and provide a fallback for non-object safe traits? I am not sure... these design decisions slow me down haha

nrxus commented 1 year ago

It looks like there's been code to support what I want in nightly for over 3 years but it has yet to be stabilized: https://github.com/rust-lang/rust/issues/43561.

There hasn't been any discussion on the tracking issue in a while though so it's hard to know if it's going to be merged soon or if it got lost in the shuffle.