如何在Rust中模拟特定的方法,但不是全部? [英] How to mock specific methods but not all of them in Rust?
问题描述
我很难确定目标结构的方法的单元测试.
I have troubles figuring out unit tests for the methods of the target struct.
我有一个方法random_number
根据结构的属性返回一个随机值,还有另一个方法plus_one
接受第一个方法的结果并对其进行处理:
I have a method random_number
that returns a random value based on the attribute of the struct and there is another method plus_one
that takes the result of the first method and does something with it:
pub struct RngTest {
pub attr: u64,
}
impl RngTest {
pub fn random_number(&self) -> u64 {
let random = 42; // lets pretend it is random
return random * self.attr;
}
pub fn plus_one(&self) -> u64 {
return self.random_number() + 1;
}
}
对第一种方法进行单元测试,对另一种方法进行测试的策略是什么?我想模拟plus_one()
的单元测试的self.random_number()
输出,使其在单元测试中具有健全的代码.有一篇不错的文章比较了不同的模拟库,并得出结论(足够可笑),它们都不是与其他人脱颖而出真的很棒.
Having a unit test for the first method, what is the strategy to test the other? I want to mock self.random_number()
output for the unit test of plus_one()
to have sane code in unit tests. There is a nice post that compares different mocking libraries and concludes (sadly enough) that none of them is really good to stand out from the others.
在阅读这些库的说明时,我唯一了解到的是,我可以模拟方法的唯一方法是将它们移至特征.我在这些库中没有看到任何示例(我查看了其中的4个或5个),它们在其中测试了与此类似的情况.
The only thing I learned while reading instructions for these libraries is that the only way I can mock methods is by moving them to a trait. I didn't see any example in these libraries (I looked at 4 or 5 of them) where they test a case similar to this.
将这些方法移至特征后(即使原样),如何模拟random_number
来对RngTest::plus_one
的输出进行单元测试?
After moving these methods to a trait (even as they are), how do I mock random_number
to unit test the output of RngTest::plus_one
?
pub trait SomeRng {
fn random_number(&self) -> u64 {
let random = 42; // lets pretend it is random
return random * self.attr;
}
fn plus_one(&self) -> u64 {
return self.random_number() + 1;
}
}
impl SomeRng for RngTest {}
推荐答案
如何模拟特定方法,但不是在Rust中模拟所有方法?
How to mock specific methods but not all of them in Rust?
正如您已经了解的那样,您不能替换类型上的方法.您唯一可以做的就是将方法移至特征,然后提供该特征的生产和特定于测试的实现.特征的结构方式决定了您可以测试的粒度.
As you have already learned, you cannot replace methods on a type. The only thing you can do is move the methods to a trait and then provide production and test-specific implementations of that trait. How you structure the trait determines the granularity of what you are able to test.
根据您的用例,您也许可以使用默认实现:
Depending on your use case, you might be able to use a default implementation:
trait SomeRng {
fn random_number(&self) -> u64;
fn plus_one(&self) -> u64 {
self.random_number() + 1
}
}
struct RngTest(u64);
impl SomeRng for RngTest {
fn random_number(&self) -> u64 {
self.0
}
}
#[test]
fn plus_one_works() {
let rng = RngTest(41);
assert_eq!(rng.plus_one(), 42);
}
在这里,random_number
是必需的方法,但是plus_one
具有默认实现.默认情况下,实现random_number
将为您提供plus_one
.如果您可以更有效地实现plus_one
,也可以选择实施.
Here, random_number
is a required method, but plus_one
has a default implementation. Implementing random_number
gives you plus_one
by default. You could also choose to implement plus_one
if you could do it more efficiently.
真正的大板条箱具有两个特征:
pub trait Rng: RngCore { /* ... */ }
RngCore
上的自动实现的扩展特征,提供了用于采样值的高级通用方法和其他便捷方法.
An automatically-implemented extension trait on
RngCore
providing high-level generic methods for sampling values and other convenience methods.
RngCore
pub trait RngCore { /* ... */ }
随机数生成器的核心.
The core of a random number generator.
这将实现的核心部分与帮助器方法分开.然后,您可以控制核心并测试助手:
This splits the core interesting parts of the implementation from the helper methods. You can then control the core and test the helpers:
trait SomeRngCore {
fn random_number(&self) -> u64;
}
trait SomeRng: SomeRngCore {
fn plus_one(&self) -> u64 {
self.random_number() + 1
}
}
impl<R: SomeRngCore> SomeRng for R {}
struct RngTest(u64);
impl SomeRngCore for RngTest {
fn random_number(&self) -> u64 {
self.0
}
}
#[test]
fn plus_one_works() {
let rng = RngTest(41);
assert_eq!(rng.plus_one(), 42);
}
这篇关于如何在Rust中模拟特定的方法,但不是全部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!