-
Hello, I am wondering if you can help me with the #[async_trait] The concrete code:
I want to test a part of the code like this:
As you can see, I call app_client.execute twice. I cannot find the syntax to say return twice with the unit tests.
Sadly I cannot find the syntax, and also, if I could execute once, it does not mock it the first call. Is it possible, or I am in the wrong direction? If there is an option, I would also like to know if I can return different things each time. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
let mut seq = Sequence::new();
let mut mock = MockAppClient::default();
mock.expect_execute()
.once()
.in_sequence(&mut seq)
.returning(|| something);
mock.expect_execute()
.once()
.in_sequence(&mut seq)
.returning(|| something_else); |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick answer and the syntax. I think I do something wrong because it does not mock it.
I try to mock AppClient, but to the method, I do not pass the mock version under test, and I think this is the problem. I should pass like MockAppClient.same_type_but_fake And when I reach this point, it executes the method and finds my_query as None.
Not sure if I use MockAll correctly or Rust at this point, but the idea of mocking is to mock the method (in this case, execute) and avoid the implementation with a fake. Do you mind telling me what to watch? If I remove all the mocks inside the test area by doing this:
file main.rs:
this syntax fails with: It seems because it is in a different file. If I put it all in one file, it is okay. |
Beta Was this translation helpful? Give feedback.
-
So is everything working now? |
Beta Was this translation helpful? Give feedback.
-
What's the problem? You said "If I put it all in one file, it is okay". Does that just mean that you are forgetting to import the mock symbols with |
Beta Was this translation helpful? Give feedback.
.times(2)
is exactly the correct syntax to use if want to expect that a function gets called twice. And if you want to return something different each time, you can use aSequence
, like this: