pcsm / simulacrum

A small library for creating mock objects in Rust.
MIT License
26 stars 3 forks source link

.called_once() violations should fail quickly #57

Open asomers opened 6 years ago

asomers commented 6 years ago

If an expectation has .called_once yet gets called more than once, it doesn't currently fail until the end of the test. This can be very confusing, especially if the following .returning statement has side effects. It would be best if the test failed immediately on the second call. For example:

    fn times_once() {
        pub trait A {
            fn foo(&self) -> u32;
        }

        create_mock! {
            impl A for AMock (self) {
                expect_foo("foo"):
                fn foo(&self) -> u32;
            }
         }

        let mut mock = AMock::new();
        let mut o = Some(0);
        mock.expect_foo()
            .called_once()
            .returning(move |_| o.take().unwrap());

        mock.foo();
        mock.foo();
    }

This code will fail with the error

panicked at 'called `Option::unwrap()` on a `None` value'

instead of something about foo being called too many times.

jasongrlicky commented 6 years ago

Thanks for reporting this, @asomers!