我尝试在响应中运行Content-Type
的集成测试。它失败了,错误为:
--> tests\greet.rs:18:9
|
5 | let response = client
| -------- move occurs because `response` has type `Response`, which does not implement the `Copy` trait
...
13 | response.text().await.unwrap(),
| ------ `response` moved due to this method call
...
18 | response.content_length(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `response`
--> C:\Users\Saurabh Mishra\.cargo\registry\src\github.com-1ecc6299db9ec823\reqwest-0.11.12\src\async_impl\response.rs:146:23
|
146 | pub async fn text(self) -> crate::Result<String> {
| ^^^^
当我注解掉响应主体(response.text()...
)的测试时,所有测试都能正确执行。
测试套件为:
#[tokio::test]
async fn greeting_works() {
spawn_app();
let client = reqwest::Client::new();
let response = client
.get("http://127.0.0.1:8080/hello")
.send()
.await
.expect("Failed to execute request");
assert!(response.status().is_success(), "Endpoint validity");
assert_eq!(
response.text().await.unwrap(),
"Hello, World!",
"Response from endpoint"
);
assert_eq!(
response.content_length(),
Some(13),
"Response length is 13 characters"
);
assert_eq!(
response.headers().get("Content-Type").unwrap(),
"text/plain; charset=utf-8"
);
}
fn spawn_app() {
let server = mailrocket::run().expect("Failed to bind address");
let _ = tokio::spawn(server);
}
我如何运行这个套件,以便执行所有四个测试?
1条答案
按热度按时间3ks5zfa01#
.text()
消耗了响应,因此在调用该方法后就不能再使用它了。一个简单的解决方法是最后对.text()
进行Assert: