[][src]Struct faux::WhenOnce

pub struct WhenOnce<'q, I, O> { /* fields omitted */ }

Similar to When but only mocks once.

Mock closures may consume captured variables as the mock will not be called more than once.

Implementations

impl<'q, I, O> WhenOnce<'q, I, O>[src]

pub unsafe fn then(self, mock: impl FnOnce(I) -> O + Send)[src]

A mirror of When.then but the mock may consume captured variables.

Safety

See When.then's safety

Usage

#[faux::create]
pub struct Foo {}

#[faux::methods]
impl Foo {
    pub fn single_arg(&self, a: &u8) -> Vec<i8> {
      /* implementation code */
    }
}

fn main() {
  let mut mock = Foo::faux();

  let vec = vec![25];
  //moves vec to the closure
  unsafe { faux::when!(mock.single_arg).once().then(|_| vec); }
  assert_eq!(mock.single_arg(&8), vec![25]);
}

pub fn then_do(self, mock: impl FnOnce() -> O + 'static + Send) where
    O: 'static, 
[src]

A mirror of When.then_do but the mock may consume captured variables.

Usage

#[faux::create]
pub struct Foo {}

#[faux::methods]
impl Foo {
    pub fn single_arg(&self, a: &u8) -> Vec<i8> {
      /* implementation code */
    }
}

fn main() {
  let mut mock = Foo::faux();

  let vec = vec![25];
  //moves vec to the closure
  faux::when!(mock.single_arg).once().then_do(|| vec);
  assert_eq!(mock.single_arg(&8), vec![25]);
}

pub fn then_return(self, mock: O) where
    O: 'static + Send
[src]

A mirror of When.then_return but the object does not need to be cloneable.

Usage

// this does not implement Clone
#[derive(PartialEq, Eq, Debug)]
pub struct NonCloneableData(i32);

#[faux::create]
pub struct Foo {}

#[faux::methods]
impl Foo {
    pub fn single_arg(&self, a: &u8) -> NonCloneableData {
      /* implementation code */
    }
}

fn main() {
  let mut mock = Foo::faux();

  faux::when!(mock.single_arg).once().then_return(NonCloneableData(2));
  assert_eq!(mock.single_arg(&8), NonCloneableData(2));
}

pub fn safe_then(self, mock: impl FnOnce(I) -> O + 'static + Send) where
    I: 'static,
    O: 'static, 
[src]

A mirror of When.safe_then but the mock may consume captured variables.

Usage

#[faux::create]
pub struct Foo {}

#[faux::methods]
impl Foo {
    pub fn single_arg(&self, a: u8) -> Vec<i8> {
      /* implementation code */
    }
}

fn main() {
  let mut mock = Foo::faux();

  let vec = vec![25];
  //moves vec to the closure
  faux::when!(mock.single_arg).once().safe_then(|_| vec);
  assert_eq!(mock.single_arg(8), vec![25]);
}

Auto Trait Implementations

impl<'q, I, O> !RefUnwindSafe for WhenOnce<'q, I, O>

impl<'q, I, O> !Send for WhenOnce<'q, I, O>

impl<'q, I, O> !Sync for WhenOnce<'q, I, O>

impl<'q, I, O> Unpin for WhenOnce<'q, I, O>

impl<'q, I, O> !UnwindSafe for WhenOnce<'q, I, O>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.