问题描述
我在为目标结构的方法确定单元测试时遇到了麻烦.
我有一个方法 random_number
根据结构的属性返回一个随机值,还有另一个方法 plus_one
采用第一个方法的结果并执行有它的东西:
pub struct RngTest {酒馆属性:u64,}实现 RngTest {pub fn random_number(&self) ->u64 {让随机= 42;//假设它是随机的返回随机 * self.attr;}pub fn plus_one(&self) ->u64 {返回 self.random_number() + 1;}}
对第一种方法进行单元测试,测试另一种方法的策略是什么?我想为 plus_one()
的单元测试模拟 self.random_number()
输出,以便在单元测试中拥有健全的代码.有一篇很好的帖子比较了 不同的模拟库 并得出结论(很遗憾)它们都不是从其他人中脱颖而出真的很好.
我在阅读这些库的说明时学到的唯一一件事是,我可以模拟方法的唯一方法是将它们移动到一个 trait.我在这些库中没有看到任何示例(我查看了其中的 4 或 5 个),它们测试了与此类似的案例.
在将这些方法移动到特征(即使它们是)之后,我如何模拟 random_number
以对 RngTest::plus_one
的输出进行单元测试?
pub trait SomeRng {fn random_number(&self) ->u64 {让随机= 42;//假设它是随机的返回随机 * self.attr;}fn plus_one(&self) ->u64 {返回 self.random_number() + 1;}}impl SomeRng for RngTest {}
正如您已经了解到的,您不能替换类型上的方法.您唯一能做的就是将方法移动到特征,然后提供该特征的生产和测试特定的实现.您如何构建特征决定了您能够测试的粒度.
具有默认实现的特征
根据您的用例,您也许可以使用默认实现:
trait SomeRng {fn random_number(&self) ->u64;fn plus_one(&self) ->u64 {self.random_number() + 1}}结构 RngTest(u64);为 RngTest 实现 SomeRng {fn random_number(&self) ->u64 {自我.0}}#[测试]fn plus_one_works() {让 rng = RngTest(41);assert_eq!(rng.plus_one(), 42);}
这里,random_number
是必需的方法,但plus_one
有一个默认实现.实现 random_number
默认为您提供 plus_one
.如果可以更有效地执行,您也可以选择实现 plus_one
.
真正的 rand crate 有什么作用?
真正的rand crate 使用两个特征:
pub trait Rng: RngCore {/* ... */}
RngCore
上自动实现的扩展特征,为采样值和其他便利方法提供高级通用方法.pub trait RngCore {/* ... */}
随机数生成器的核心.
这将实现的核心有趣部分与辅助方法分开.然后您可以控制核心并测试助手:
trait SomeRngCore {fn random_number(&self) ->u64;}特征 SomeRng: SomeRngCore {fn plus_one(&self) ->u64 {self.random_number() + 1}}impl<R: SomeRngCore>用于 R {} 的 SomeRng结构 RngTest(u64);为 RngTest 实现 SomeRngCore {fn random_number(&self) ->u64 {自我.0}}#[测试]fn plus_one_works() {让 rng = RngTest(41);assert_eq!(rng.plus_one(), 42);}
I have troubles figuring out unit tests for the methods of the target struct.
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;
}
}
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.
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.
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 {}
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.
Trait with a default implementation
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);
}
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.
What does the real rand crate do?
The real rand crate uses two traits:
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 中模拟特定方法但不是所有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!