All Projects → pcsm → Simulacrum

pcsm / Simulacrum

Licence: mit
A small library for creating mock objects in Rust.

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Simulacrum

xfg-rs
eXtensible Framegraph
Stars: ✭ 34 (+61.9%)
Mutual labels:  rust-library
Redbpf
Rust library for building and running BPF/eBPF modules
Stars: ✭ 611 (+2809.52%)
Mutual labels:  rust-library
Superslice Rs
Extensions for ordered Rust slices.
Stars: ✭ 17 (-19.05%)
Mutual labels:  rust-library
Lingua Rs
👄 The most accurate natural language detection library in the Rust ecosystem, suitable for long and short text alike
Stars: ✭ 260 (+1138.1%)
Mutual labels:  rust-library
Printpdf
An easy-to-use library for writing PDF in Rust
Stars: ✭ 404 (+1823.81%)
Mutual labels:  rust-library
Lopdf
A Rust library for PDF document manipulation.
Stars: ✭ 720 (+3328.57%)
Mutual labels:  rust-library
poem
A full-featured and easy-to-use web framework with the Rust programming language.
Stars: ✭ 1,167 (+5457.14%)
Mutual labels:  rust-library
Tract
Tiny, no-nonsense, self-contained, Tensorflow and ONNX inference
Stars: ✭ 899 (+4180.95%)
Mutual labels:  rust-library
Ureq
Minimal request library in rust.
Stars: ✭ 545 (+2495.24%)
Mutual labels:  rust-library
Rage
A simple, secure and modern encryption tool (and Rust library) with small explicit keys, no config options, and UNIX-style composability.
Stars: ✭ 826 (+3833.33%)
Mutual labels:  rust-library
Gba
A crate that helps you make GBA games
Stars: ✭ 286 (+1261.9%)
Mutual labels:  rust-library
Juniper
GraphQL server library for Rust
Stars: ✭ 4,187 (+19838.1%)
Mutual labels:  rust-library
Not Yet Awesome Rust
A curated list of Rust code and resources that do NOT exist yet, but would be beneficial to the Rust community.
Stars: ✭ 789 (+3657.14%)
Mutual labels:  rust-library
gui
A generic UI library/framework.
Stars: ✭ 16 (-23.81%)
Mutual labels:  rust-library
Libimagequant Rust
libimagequant (pngquant) bindings for the Rust language
Stars: ✭ 17 (-19.05%)
Mutual labels:  rust-library
libjail-rs
Rust implementation of a FreeBSD jail library
Stars: ✭ 32 (+52.38%)
Mutual labels:  rust-library
Quicksilver
A simple framework for 2D games on desktop and web
Stars: ✭ 710 (+3280.95%)
Mutual labels:  rust-library
Rust Csv
A CSV parser for Rust, with Serde support.
Stars: ✭ 911 (+4238.1%)
Mutual labels:  rust-library
Rusticsom
Rust library for Self Organising Maps (SOM).
Stars: ✭ 18 (-14.29%)
Mutual labels:  rust-library
Argh
Rust derive-based argument parsing optimized for code size
Stars: ✭ 803 (+3723.81%)
Mutual labels:  rust-library

Simulacrum Docs Crates.io

A minimal library for creating mock objects by hand using stable Rust.

To install, add this line to your Cargo.toml:

[dependencies]
simulacrum = "0.3.0"

Note that this crate has not yet reached version 1.0, so the API may change drastically between releases.

Using Mock Objects

Simulacrum mock objects provide a consistent interface that features call counts, parameter matching, return values, and modifying parameters by mutable reference.

// Create a mock object
let mut mock = CoolTraitMock::new();

// Set up expectations for it
mock.expect_foo()
    .called_once();
mock.then()
    .expect_goop()
    .called_once()
    .with(true)
    .returning(|_| 5);

// Execute test code
m.foo();
assert_eq!(m.goop(true), 5);

// When the mock object is dropped, its expectations will be evaluated

See macros_high.rs for a full run-through of the mock object user API.

Creating Mock Objects

Simulacrum provides several APIs at different levels of abstraction, so you can create mock objects with the level of control you desire. All mock objects created with Simulacrum expose the same user API, no matter which API level is used to create them.

The following examples all show how to mock this trait:

trait CoolTrait {
    fn foo(&self);
    fn goop(&mut self, flag: bool) -> u32;
    fn store(&self, val: &i64);
}

Note that the macro API only supports creating mocks from a trait, while the manual API allows you to create mock objects to stand in for structs as well.

High-Level Macro Example

The create_mock! macro creates a mock object from a trait. Just copy over the trait's interface and annotate it:

extern crate simulacrum;

use simulacrum::*;

create_mock! {
    impl CoolTrait for CoolTraitMock (self) {
        expect_foo("foo"):
        fn foo(&self);

        expect_goop("goop"):
        fn goop(&mut self, flag: bool) -> u32;

        // & params are mocked as *const and &mut are mocked as *mut.
        expect_store("store"):
        fn store(&self, val: &i64);
    }
}

See macros_high.rs for more examples of how to mock out different types of methods with create_mock!.

Mid-Level Macros Example

If you need more control than the high-level macro offers, you can use the create_mock_struct! and was_called! macros. This is useful if you'd like to create mock objects with features that the high-level macro doesn't support, like generic methods. Note that you can mix-and-match these macros with the manual interface as well.

extern crate simulacrum;

use simulacrum::*;

create_mock_struct! {
    struct CoolTraitMock: {
        expect_foo("foo");
        expect_goop("goop") bool => u32;
        // Note that we've used *const instead of & for shared references.
        expect_store("store") *const i64;
    }
}

impl CoolTrait for CoolTraitMock {
    fn foo(&self) {
        was_called!(self, "foo")
    }

    fn goop(&mut self, flag: bool) -> u32 {
        was_called!(self, "goop", (flag: bool) -> u32)
    }

    fn store(&self, val: &i64) {
        // Again note the use of *const instead of & for shared references.
        was_called!(self, "store", (val: *const i64))
    }
}

See macros_mid.rs for more examples of how to mock out different types of methods with the Mid-Level Macros.

Manual Example

Create your mock objects manually for ultimate control. With this API, you can even create mocks to stand in for structs instead of traits. For a detailed example of the API, see the manual.rs example.

extern crate simulacrum;

use simulacrum::*;

trait CoolTrait {
    fn foo(&self);
    fn goop(&mut self, flag: bool) -> u32;
}

pub struct CoolTraitMock {
    e: Expectations
}

impl CoolTraitMock {
    pub fn new() -> Self {
        Self {
            e: Expectations::new()
        }
    }

    pub fn then(&mut self) -> &mut Self {
        self.e.then();
        self
    }

    pub fn expect_foo(&mut self) -> Method<(), ()> {
        self.e.expect::<(), ()>("foo")
    }

    pub fn expect_goop(&mut self) -> Method<bool, u32> {
        self.e.expect::<bool, u32>("goop")
    }
}

impl CoolTrait for CoolTraitMock {
    fn foo(&self) {
        self.e.was_called::<(), ()>("foo", ())
    }

    fn goop(&mut self, flag: bool) -> u32 {
        self.e.was_called_returning::<bool, u32>("goop", flag)
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].