Skip to content

Instantly share code, notes, and snippets.

@CodeSandwich
Last active June 21, 2018 16:25
Show Gist options
  • Save CodeSandwich/82e25d91f4868348adbeff356f49dfa8 to your computer and use it in GitHub Desktop.
Save CodeSandwich/82e25d91f4868348adbeff356f49dfa8 to your computer and use it in GitHub Desktop.
#![cfg_attr(test, feature(proc_macro, proc_macro_mod))]
#[cfg(test)] // It's needed only for test runs
extern crate mocktopus;
#[cfg(test)]
mod tests {
use mocktopus::macros::*; // Lack of that line was crashing your code :)
use mocktopus::mocking::*;
// annotating a struct does nothing
// #[cfg_attr(test, mockable)]
pub struct Real;
// annotating an impl block does EVERYTHING, it lets mock functions inside
// BTW consider annotating `mod tests {`, the macro will find `sayit` inside and will make it mockable
#[cfg_attr(test, mockable)]
impl Real {
pub fn sayit(&self) -> u32 {
1
}
}
#[test]
fn it_works() {
let r = Real{};
// mock_raw is needed only for special cases, where the closure
// does not have static lifetime (e.g. uses a reference to a local variable)
// use mock_safe wherever you can!
Real::sayit.mock_safe(|_| MockResult::Return(0));
assert_eq!(r.sayit(), 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment