simulacrum icon indicating copy to clipboard operation
simulacrum copied to clipboard

panicked at 'called `Option::unwrap()` on a `None` value' when mocking generic methods

Open asomers opened this issue 7 years ago • 1 comments

I can use Simulacrum to mock a method with generic parameters, and to mock a method with a generic return value, but I seemingly can't do both at the same time. Here's the problematic code:

pub trait A {
    fn foo<T: 'static>(&self, t:T);
    fn bar<T: 'static>(&self) -> T;
}

create_mock_struct! {
    struct AMock: {
        expect_foo_i16("foo") i16;
        expect_foo_u32("foo") u32;
        expect_bar_i16("bar") () => i16;
        expect_bar_u32("bar") () => u32;
    }
}

impl A for AMock {
    fn foo<T: 'static>(&self, t: T) {
        was_called!(self, "foo", (t: T))
    }

    fn bar<T: 'static>(&self) -> T {
        was_called!(self, "bar", () -> T)
    }
}

let mut mock: AMock = AMock::new();
mock.expect_foo_i16().called_once().with(-1);
mock.expect_bar_i16().called_once().returning(|_| -5);
mock.then().expect_foo_u32().called_once().with(1);
mock.expect_bar_u32().called_once().returning(|_| 1_000_000);

mock.foo::<i16>(-1);
mock.foo::<u32>(1);
assert_eq!(-5, mock.bar::<i16>());
assert_eq!(1_000_000, mock.bar::<u32>());

And here's the error:

---- t_simulacrum::t::generic_method stdout ----
        thread 't_simulacrum::t::generic_method' panicked at 'called `Option::unwrap()` on a `None` value', libcore/option.rs:335:21
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
   1: std::sys_common::backtrace::print
   2: std::panicking::default_hook::{{closure}}
   3: std::panicking::default_hook
   4: std::panicking::rust_panic_with_hook
   5: std::panicking::begin_panic_fmt
   6: rust_begin_unwind
   7: core::panicking::panic_fmt
   8: core::panicking::panic
   9: <core::option::Option<T>>::unwrap
             at /checkout/src/libcore/macros.rs:20
  10: <simulacrum_mock::store::ExpectationMatcher<'a, I, O>>::was_called
             at /usr/home/somers/.cargo/registry/src/github.com-1ecc6299db9ec823/simulacrum_mock-0.1.0/src/store.rs:224
  11: simulacrum_mock::mock::Expectations::was_called
             at /usr/home/somers/.cargo/registry/src/github.com-1ecc6299db9ec823/simulacrum_mock-0.1.0/src/mock.rs:51
  12: <<mock_shootout::t_simulacrum::t::Simulacrum as mock_shootout::TestSuite>::generic_method::AMock as <mock_shootout::t_simulacrum::t::Simulacrum as mock_shootout::TestSuite>::generic_method::A>::foo
             at src/t_simulacrum.rs:194
  13: <mock_shootout::t_simulacrum::t::Simulacrum as mock_shootout::TestSuite>::generic_method
             at src/t_simulacrum.rs:209
  14: mock_shootout::t_simulacrum::t::generic_method
             at src/lib.rs:28
  15: mock_shootout::__test::TESTS::{{closure}}
             at src/lib.rs:28
  16: core::ops::function::FnOnce::call_once
             at /checkout/src/libcore/ops/function.rs:223
  17: <F as alloc::boxed::FnBox<A>>::call_box
  18: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:105

asomers avatar May 23 '18 22:05 asomers

Oh, that’s really interesting - thanks for reporting this, I’ll look into it!

jasongrlicky avatar May 23 '18 22:05 jasongrlicky