Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock a trait with a fluent interface #49

Open
neugepower opened this issue Jun 9, 2019 · 1 comment
Open

Mock a trait with a fluent interface #49

neugepower opened this issue Jun 9, 2019 · 1 comment

Comments

@neugepower
Copy link

I try to configure a mock factory which is implemented as fluent interface:

#[cfg_attr(test, mocked)]
pub trait IdFactory {
    fn with_value(&mut self, value: usize) -> &mut dyn IdFactory;
    fn build(&mut self) -> Box<dyn Id>;
}

My code under test:

...
let id = id_factory.with_id(0).build();
...

And my expectations:

...
let (id_factory_mock, id_factory_mock_handle) = scenario.create_mock_for::<dyn IdFactory>();
scenario.expect(id_factory_mock_handle.with_value(0).and_return(XXX));
...

For the XXX part I don't now what to return. I tried several things but I'm not sure if it is even possible at all right now. I thought of sth. like this:

scenario.expect(id_factory_mock_handle.with_value(0).then().build().and_return(Box::new(MyId)));

@kriomant
Copy link
Owner

kriomant commented Jul 6, 2019

I'm working on it. It's not so easy to do it without breaking things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants