gpt4 book ai didi

How to change base URL in a Mockito test in Rust?(如何在Rust的Mockito测试中更改基本URL?)

转载 作者:bug小助手 更新时间:2023-10-28 14:01:00 27 4
gpt4 key购买 nike



I am relatively new to Rust and I am trying to write a few unit tests for my web service using Mockito.

我对Rust比较陌生,我正在尝试使用Mockito为我的Web服务编写一些单元测试。


In Mockito's documentation, they have the following example:

在Mockito的文档中,他们有以下示例:


#[cfg(test)]
mod tests {
#[test]
fn test_something() {
// Request a new server from the pool
let mut server = mockito::Server::new();

// Use one of these addresses to configure your client
let host = server.host_with_port();
let url = server.url();

// Create a mock
let mock = server.mock("GET", "/hello")
.with_status(201)
.with_header("content-type", "text/plain")
.with_header("x-api-key", "1234")
.with_body("world")
.create();

// Any calls to GET /hello beyond this line will respond with 201, the
// `content-type: text/plain` header and the body "world".

// You can use `Mock::assert` to verify that your mock was called
// mock.assert();
}
}

In the function I am trying to test, there are these lines that I want to mock:

在我试图测试的函数中,有一些我想模仿的行:


    let client = reqwest::Client::new();
let result_response = client.post(Url::parse("https://libretranslate.org/translate").unwrap()).headers(headers).body(body)
.send().await;


I cannot wrap my head around how to test my function that contains libretranslate.org, while tests contain localhost:<random_port>.

当测试包含Localhost: 时,我无法理解如何测试包含libreTransate.org的函数。


Should I use some kind of config file that specifies which base URL should be used while running tests?

我是否应该使用某种配置文件来指定在运行测试时应该使用哪个基URL?


I cannot find any article/thread/question online about Mockito in Rust with a complete example to have a look at, and I am struggling to use it.

我在网上找不到任何关于Mockito in Rust的文章/帖子/问题,还有一个完整的例子可以看一看,我正在努力地使用它。


更多回答

You shouldn't hardcode the url, instead, why don't you pass it as parameter?

你不应该硬编码url,为什么不把它作为参数传递呢?

@cafce25 that is a workaround for sure, but I didn't want to change the implementation just for the tests. Because if then you remove tests from the equation, I end up using a parameter for nothing. Btw I use a const with the URL, I added it in the example like that, so that I won't need to add extra lines of code.

@afce25这当然是一种变通办法,但我不想仅仅为了测试而更改实现。因为如果你从方程式中去掉测试,我最终会一无所获地使用一个参数。顺便说一句,我将常量与URL一起使用,我将其添加到示例中,这样我就不需要添加额外的代码行了。

Any other solution would either need to modify the code under test or the hosts domain name resolution neither of which is a thing a test should do.

任何其他解决方案要么需要修改测试中的代码,要么需要修改主机域名解析,而这两者都不是测试应该做的事情。

Tangent: in my experience you should always prefer making the URLs (scheme, domain, and base route) configurable. It doesn't matter how confident you are that the endpoint will never change, life will prove you wrong at some point (via changing domains, using a new provider, adding a proxy, etc.). I've had to rip out hard-coded URLs more often than I can count.

切线:根据我的经验,你应该总是喜欢让URL(方案,域和基本路由)可配置。无论您对端点永远不会改变有多自信,生活都会在某个时候证明您是错误的(通过更改域,使用新的提供商,添加代理等)。我不得不经常撕掉硬编码的URL,次数多得我都数不清了。

优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com