-
Notifications
You must be signed in to change notification settings - Fork 69
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
Call count for free functions requires manual checkpoint #30
Comments
Nope, that's not intended. It's a bug, but it's really just one symptom of a larger problem: mocked free functions have no context. Mocked methods can use their object for context, but free functions cannot. In addition to the #[test]
fn foo() {
expect_open().returning(|_| Err(EBADF));
...
}
fn bar() {
expect_open().with(eq("/etc/passwd")).returning(|_| MockFileDescriptor::new())
...
open("/etc/passwd").unwrap();
...
} If the
#[test]
fn foo() {
let s = MockScenario::new();
let e = expect_open().with(eq("/etc/passwd")).return_const(Err(EPERM));
s.push(e);
...
}
#[test]
fn foo() {
let c: MockContext = expect_open();
c.expect().with(eq("/etc/passwd")).return_const(Err(EPERM));
...
} What do you think? I personally don't need to mock free functions very often, so I haven't given it much thought. |
I don't know yet which solution would be the best, but I think it should take into account another problem mentioned here - #31. Could any of the solutions presented by you solve it? |
No need. I think that other issue can be solved completely independently. |
It would be great if a final solution was easy to use correctly and hard to misuse. From the proposals above it seems the 3rd one has the most potential. The MockContext object could be marked with It looks like the other proposals make creating context guards optional, which increases the discipline required to write and maintain the test code. |
This commit adds Context objects for static methods and free functions. Expectations are still global, but the context object will clean up any global expectations when it drops. This has a few benefits: * Call counts will be verified when the context object drops * The context object can be used to checkpoint its method * "No matching expectation found" errors will no longer poison the static method's global mutex. Panics in the returning method still will, however. Fixes #30
This commit adds Context objects for static methods and free functions. Expectations are still global, but the context object will clean up any global expectations when it drops. This has a few benefits: * Call counts will be verified when the context object drops * The context object can be used to checkpoint its method * "No matching expectation found" errors will no longer poison the static method's global mutex. Panics in the returning method still will, however. Fixes #30
This commit adds Context objects for static methods and free functions. Expectations are still global, but the context object will clean up any global expectations when it drops. This has a few benefits: * Call counts will be verified when the context object drops * The context object can be used to checkpoint its method * "No matching expectation found" errors will no longer poison the static method's global mutex. Panics in the returning method still will, however. Fixes #30
This commit adds Context objects for static methods and free functions. Expectations are still global, but the context object will clean up any global expectations when it drops. This has a few benefits: * Call counts will be verified when the context object drops * The context object can be used to checkpoint its method * "No matching expectation found" errors will no longer poison the static method's global mutex. Panics in the returning method still will, however. Fixes #30
This commit adds Context objects for static methods and free functions. Expectations are still global, but the context object will clean up any global expectations when it drops. This has a few benefits: * Call counts will be verified when the context object drops * The context object can be used to checkpoint its method * "No matching expectation found" errors will no longer poison the static method's global mutex. Panics in the returning method still will, however. Fixes #30
I wrote a simple example to see if I could mock external modules with free functions. I noticed that
times(x)
is not checked on expectations for free functions unless I manually addedcheckpoint()
.Here's a snippet for mocking
std::env::current_dir
:Is it an intended behavior, have I missed something or is it a bug? If the first, the requirement of using
checkpoint()
could be added to the docs of modules - https://docs.rs/mockall/0.3.0/mockall/#modulesThe text was updated successfully, but these errors were encountered: