galvanic-mock
galvanic-mock copied to clipboard
How to mock the Read trait?
Hi, After trying a few things I seem to be at a loss on how to mock the Read trait for the arguments.
pub trait GetToken {
fn tokenize(&mut self) -> Vec<Token>;
fn gettok<T: Read>(&mut self, stream: &mut T) -> Token; // I WANT TO MOCK STREAM
}
But even before I try to test the structure implementing GetToken I am running into a wall trying to mock Read. I've tried a few variations already that are not working:
extern crate galvanic_mock;
use galvanic_mock::{mockable, use_mocks};
extern crate kaleidoscope;
use kaleidoscope::lexer::{GetToken, Lexer};
use std::collections::HashSet;
use std::io::Read;
// First declare the trait to be mockable
#[mockable]
trait MockRead: Read {}
mod test {
use super::*;
// Next say use_mocks in functions where you intend to use Read mock.
#[test]
#[use_mocks]
fn test_foo() {
let mut mock = new_mock!(MockRead);
given! {
<mock as MockRead>::read(|buf: &mut [u8]| buf.len()==1 && buf[0].is_ascii_alphanumeric()) then_return Ok(1) always;
}
// Do assertions by using this mock.
}
}
Here is the error I get:
error[E0277]: the trait bound `test::mod_test_foo::mock::Mock33: std::io::R
ead` is not satisfied
--> tests/lexer.rs:19:5
|
19 | #[use_mocks]
| ^^^^^^^^^^^^ the trait `std::io::Read` is not implemented for `tes
t::mod_test_foo::mock::Mock33`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: Could not compile `kaleidoscope_rust`.
Another one I tried that did not work was:
extern crate galvanic_mock;
use galvanic_mock::{mockable, use_mocks};
extern crate kaleidoscope;
use kaleidoscope::lexer::{GetToken, Lexer};
use std::collections::HashSet;
use std::io::Read;
// First declare the trait to be mockable
mod test {
use super::*;
// Next say use_mocks in functions where you intend to use Read mock.
#[test]
#[use_mocks]
fn test_foo() {
let mut mock = new_mock!(Read);
given! {
<mock as MockRead>::read(|buf: &mut [u8]| buf.len()==1 && buf[0].is_ascii_alphanumeric()) then_return Ok(1) always;
}
// Do other things
}
}
I get the error I get:
>> cargo test -- --nocapture
Compiling kaleidoscope_rust v0.1.0 (/Volumes/Macintosh HD/Dropbox/works
ace/kaleidoscope_rust)
error: custom attribute panicked
--> tests/lexer.rs:18:5
|
18 | #[use_mocks]
| ^^^^^^^^^^^^
|
= help: message: All mocked traits must be defined using 'mockable!': `
ead` not found in
error: aborting due to previous error
Any ideas on what might work?