无法将“&Thing”与“Thing”进行比较

2024-04-19

我知道该错误意味着什么,但我无法修复它。我在用着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"

你不能比较一个Thing to a &Thing使用默认推导PartialEq:

#[derive(Debug, PartialEq)]
struct Thing(String);

fn main() {
    let t_val = Thing(String::new());
    let t_ref = &t_val;

    t_val == t_ref;
}
error[E0308]: mismatched types
 --> src/main.rs:8:14
  |
8 |     t_val == t_ref;
  |              ^^^^^ expected struct `Thing`, found &Thing
  |
  = note: expected type `Thing`
             found type `&Thing`

要修复该错误,您需要执行以下两项操作之一:

  1. 匹配参考电平:

    • t_val == *t_ref

    • &t_val == t_ref

  2. 对不匹配数量的引用实现相等:

    impl<'a> PartialEq<&'a Thing> for Thing {
        fn eq(&self, other: &&'a Thing) -> bool {
            self == *other
        }
    }
    
    impl<'a> PartialEq<Thing> for &'a Thing {
        fn eq(&self, other: &Thing) -> bool {
            *self == other
        }
    }
    

然而,这些都不能解决问题你的实际问题。您误解了嘲笑者库的工作原理;您的闭包采用了错误的参考级别,并且它需要获取要比较的值的所有权:

let expected_thing = thing.clone();
scenario.expect(
    dao.get_call(check(move |t: &&Thing| t == &&expected_thing))
        .and_return("hello".to_string()),
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法将“&Thing”与“Thing”进行比较 的相关文章

随机推荐