Skip to content

How to use murf without chaning my code? #8

@szabgab

Description

@szabgab

Hi,

as I understand it in all the examples in the README, the code in main() was using some murf-code. e.g. let (handle, mock) = MyStruct::mock_with_handle();. Is it possible to use murf without changing my application code. Only in the test code? e.g. here is a "real world" example where one function takes a long time to run. I'd like replace it during testing so the test would run faster:

pub fn add(left: u64, right: u64) -> u64 {
    long();
    left + right
}

pub fn long() {
    std::thread::sleep(std::time::Duration::from_secs(20));
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        // replace the long() function to return immediately
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

And another version of the code, this time using a struct:

#![allow(dead_code)]

struct Rectangle {
    length: u32,
    width: u32,
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.long();
        self.width * self.length
    }
}

impl Rectangle {
    fn long(&self) {
        std::thread::sleep(std::time::Duration::from_secs(20)); 
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let r = Rectangle { length: 2, width: 3};
        assert_eq!(r.area(), 6);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions