testing - Rust:替换结构上的函数
问题描述
我有以下代码,我正在尝试测试。我的问题是如何替换在 B 上实现的功能?所以在测试 B.something 什么都不做,或者返回一个指定的值?
trait DoSomething {
fn something(&mut self);
}
struct A {}
struct B {}
impl DoSomething for A {
fn something(&mut self) {
println!("doing something on A");
let mut b = B{};
b.something();
}
}
impl DoSomething for B {
fn something(&mut self) {
println!("doing something on B");
}
}
fn main() {
let mut a = A{};
a.something();
}
上面的代码可以在rust 操场上找到
来自 python 我会简单地 patch.object(b, 'something')
解决方案
推荐阅读
- arrays - 调整向量大小 Julia
- java - 在 applicationContext.xml 中添加 bean 'sessionFactory' 后出错
- android - 如何创建服务 FirebaseMessagingService 在后台始终处于活动状态
- python - 使用 DateTimeIndex 计算数据框中字符串的出现次数
- tensorflow - tensorflow 急切和命令式自定义层
- prolog - Prolog 在数组/列表中显示结果
- mysql - mysql字符串变成IN子句分割字符串什么的?
- r - RStudio 无法加载 web 文件,但终端中的 R 可以;为什么?
- spring-boot - SpringBoot-Websphere-Java Config-web.xml-resource-ref
- tensorflow - TensorFlow 会话失败,错误未编译为使用 AVX2