pub struct KubernetesMocker { /* private fields */ }
Expand description
The main mocker struct. Holds all the information about expected API calls, and provides
functionality to set up expected requests and responses before testing with .run()
.
Implementations§
Source§impl KubernetesMocker
impl KubernetesMocker
Sourcepub async fn expect<'a, Fut, T, F>(
&mut self,
expected_api_call: F,
result: MockReturn<'a, T>,
) -> Result<&mut Self, MockError>
pub async fn expect<'a, Fut, T, F>( &mut self, expected_api_call: F, result: MockReturn<'a, T>, ) -> Result<&mut Self, MockError>
mocker.expect()
- takes a closure producing a future and what to return when receiving
the API call given by the closure.
Each closure should only have 1 API call. More will be ignored, fewer will return an error.
Will return an error on panic, timeout, or if the closure does not make a request - feel
free to use unwrap()
liberally!
Returns Ok(&mut self)
on success, meaning you can chain
.expect().await.unwrap().expect().await.unwrap()
…
§Errors
MockError::Serde
-serde_json
failed to serialize mock responseMockError::NoRequest
- closure passed in did not make an API request.MockError::Timeout
- closure passed in timed out.MockError::Panicked
- closure passed in panicked.MockError::WatchNoItems
- Watch events list must contain at least oneWatchEvent
.MockError::HttpBody
- failed to create anhttp::Response
from the serialized bytes.
§Examples
let (client, mut mocker) = make_mocker();
let api: Api<Node> = Api::all(client);
mocker.expect(|| async {
api.list(&ListParams::default()).await.unwrap();
},
MockReturn::List(&[Node::default()]),
).await.unwrap()
.expect(|| async {
api.create(&PostParams::default(), &Node::default()).await.unwrap();
},
MockReturn::Single(Node::default()),
).await.unwrap();
Sourcepub async fn run(self) -> Result<(), MockRunError>
pub async fn run(self) -> Result<(), MockRunError>
KubernetesMocker::run()
- produces a future which will compare the received API calls
past this point, and compare them to the ones received during the expect()
calls.
§Errors
MockRunError::TooFewApiCalls
- it did not receive as many API calls asexpect()
got.MockRunError::TooManyApiCalls
- received at least 1 more API call that was not expected.MockRunError::IncorrectApiCall
- an API call did not match the one received in the correspondingexpect()
call.
Should be run with tokio::task::spawn
so it runs concurrently with whatever is being tested.
§Examples
See examples for make_mocker()
.
§Panics
Should not panic, if an HTTP body fails to be made it should be presented as an error
during expect()
. Still uses unwrap()
for brevity, and to avoid a duplicate of
MockError::HttpBody
in MockRunError
.
Auto Trait Implementations§
impl Freeze for KubernetesMocker
impl !RefUnwindSafe for KubernetesMocker
impl Send for KubernetesMocker
impl Sync for KubernetesMocker
impl Unpin for KubernetesMocker
impl !UnwindSafe for KubernetesMocker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more