Я знаю, что означает ошибка, но не могу ее исправить.Я использую mockers
для проверки своей работы, и я застрял, когда пытался проверить параметр структуры, который был задан функции макетируемой черты.Упрощенный код:
#[cfg(test)]
extern crate mockers;
#[cfg(test)]
extern crate mockers_derive;
#[cfg(test)]
use mockers_derive::mocked;
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug)]
pub struct Thing {
pub key: String,
pub class: String,
}
#[cfg_attr(test, mocked)]
pub trait DaoTrait {
fn get(&self, thing: &Thing) -> String;
}
struct DataService {
dao: Box<DaoTrait>,
}
impl DataService {
pub fn get(&self, thing: &Thing) -> String {
self.dao.get(thing)
}
}
#[cfg(test)]
mod test {
use super::*;
use mockers::matchers::eq;
use mockers::Scenario;
#[test]
fn my_test() {
use mockers::matchers::check;
let scenario = Scenario::new();
let mut dao = scenario.create_mock_for::<DaoTrait>();
let thing = Thing {
key: "my test".to_string(),
class: "for test".to_string(),
};
scenario.expect(
dao.get_call(check(|t: &Thing| t.to_owned() == thing))
.and_return("hello".to_string()),
);
let testee = DataService { dao: Box::new(dao) };
let rtn = testee.get(&thing);
assert_eq!(rtn, "hello");
}
}
Я получил ошибки:
warning: unused import: `mockers::matchers::eq`
--> src/main.rs:33:9
|
33 | use mockers::matchers::eq;
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default
error[E0277]: can't compare `&Thing` with `Thing`
--> src/main.rs:47:57
|
47 | dao.get_call(check(|t: &Thing| t.to_owned() == thing))
| ^^ no implementation for `&Thing == Thing`
|
= help: the trait `std::cmp::PartialEq<Thing>` is not implemented for `&Thing`
error[E0277]: the trait bound `mockers::matchers::BoolFnMatchArg<Thing, [closure@src/main.rs:47:32: 47:65 thing:_]>: mockers::MatchArg<&Thing>` is not satisfied
--> src/main.rs:47:17
|
47 | dao.get_call(check(|t: &Thing| t.to_owned() == thing))
| ^^^^^^^^ the trait `mockers::MatchArg<&Thing>` is not implemented for `mockers::matchers::BoolFnMatchArg<Thing, [closure@src/main.rs:47:32: 47:65 thing:_]>`
|
= help: the following implementations were found:
<mockers::matchers::BoolFnMatchArg<T, F> as mockers::MatchArg<T>>
Я просмотрел исходный код check
:
pub fn check<T, F: Fn(&T) -> bool>(f: F) -> BoolFnMatchArg<T, F> {
BoolFnMatchArg { func: f, _phantom: PhantomData }
}
Я думаю, что закрытие|t: &Thing| t.to_owned() == thing
Я дал правильно.Я также попробовал следующие замыкания, но ни одно из них не сработало.
|t: &Thing| t == &thing
|t: &Thing| *t == thing
|t: Thing| t == thing
Cargo.toml:
[dev-dependencies]
mockers = "0.12.1"
mockers_derive = "0.12.1"